Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcnunes/f3d111ce05274cae7e43 to your computer and use it in GitHub Desktop.
Save maxcnunes/f3d111ce05274cae7e43 to your computer and use it in GitHub Desktop.
Script to send email by gmail using current user account
var nodemailer = require('nodemailer'),
env = require('../env');
var Service = module.exports = function () {};
Service.prototype.sendByCurrentUserEmailAccount = function(opt, user) {
var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
XOAuth2: {
user: user.email,
clientId: env.config.google.clientID,
clientSecret: env.config.google.clientSecret,
refreshToken: user.googleRefreshToken
}
}
});
if (!Array.isArray(opt.to)) opt.to = [opt.to];
var mailOptions = {
from: opt.from,
to: opt.to,
subject: opt.subject,
generateTextFromHTML: true,
html: opt.html
};
smtpTransport.sendMail(mailOptions, function(err) {
if (err) console.log(err);
smtpTransport.close();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment