To connect to Gmail, you should use the XOAuth2 token. No worries, Nodemailer already knows about this:
var smtpTransport = nodemailer.createTransport('SMTP', { service: 'Gmail', auth: { XOAuth2: { user: smtpConfig.user, clientId: smtpConfig.client_id, clientSecret: smtpConfig.client_secret, refreshToken: smtpConfig.refresh_token, accessToken: smtpConfig.access_token, timeout: smtpConfig.access_timeout - Date.now() } } };
To register your application, you need to go to the Google Cloud Console . Then you need to get access tokens for the accounts you want to use. You can use passports for this.
Here's what it looks like in my code:
var passport = require('passport'), GoogleStrategy = require('./google_oauth2'), config = require('../config'); passport.use('google-imap', new GoogleStrategy({ clientID: config('google.api.client_id'), clientSecret: config('google.api.client_secret') }, function (accessToken, refreshToken, profile, done) { console.log(accessToken, refreshToken, profile); done(null, { access_token: accessToken, refresh_token: refreshToken, profile: profile }); })); exports.mount = function (app) { app.get('/add-imap/:address?', function (req, res, next) { passport.authorize('google-imap', { scope: [ 'https://mail.google.com/', 'https://www.googleapis.com/auth/userinfo.email' ], callbackURL: config('web.vhost') + '/add-imap', accessType: 'offline', approvalPrompt: 'force', loginHint: req.params.address })(req, res, function () { res.send(req.user); }); }); };
Laurent Perrin Nov 09 '13 at 17:19 2013-11-09 17:19
source share