6

I want to ask something about nodemailer, i make code like

Var client = nodemailer.createTransport ({ 
Service: 'gmail',
Auth: {
User: 'example@gmail.com', // Your email address
Pass: '123' // Your password},
Tls: {rejectUnauthorized: false}
});

And this works, but after successful delivery, when I have to receive email messages that have been sent, I need to enable gmail settings like "Allow less secure apps to access". I do not want to set it.

So how do I send emails from example@gmail.com TO example1@gmail.com, without setting "Allow less secure apps to access" and message directly accept in email box !!??? Or any other plugin that should be added ??

THANX;)

mscdex
  • 104,356
  • 15
  • 192
  • 153

2 Answers2

6

Obtain accessToken & refreshToken from Google OAuth2.0 Playground, clientId & clientSecret from Google developer console

const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');
var express = require('express');
var router = express.Router();


var smtpTransport = nodemailer.createTransport('SMTP',{
  service:"Gmail",
  auth:{
    XOAuth2: {
      user:'sender@emailaddress.com',
      clientId: 'your-client-id',
      clientSecret: 'your-cliet-secret',
      accessToken:'your-access-token',
      refreshToken: 'your-refresh-token'

  }
  }
});

router.get('/emaildemo', function(req, res, next) {

  var mailOptions = {
  from: 'sender@emailaddress.com', 
  to: 'xxx@email.com', 
  subject: 'TEST SUBJECTT', 
  text: 'TEST MAIL', 
}; 
smtpTransport.sendMail(mailOptions, function(error, info){
  if(error){
 console.log('Error Occured', error);
  return res.send(error);
  }
  return res.send("mail send successfully");
}); 

});
module.exports = router;
vikasThakur.com
  • 578
  • 4
  • 13
  • hey thanx Vikas Thakur, after I use XOAuth2 why in the refresh section the token has an active period that is only given 2000 seconds and if it is past that time I have to refresh the token reset, and refresh the token changed how to handle it? – Adriyana Putra Pratama Sep 07 '17 at 17:58
0

You need to get the access token, you cannot give it statically. googleapis can be user for gmail. for example:

        const { google } = require('googleapis');
        const { OAuth2 } = google.auth;

        const {
            MAILING_SERVICE_CLIENT_ID,
            MAILING_SERVICE_CLIENT_SECRET,
            MAILING_SERVICE_REFRESH_TOKEN,
            SENDER_EMAIL_ADDRESS,
            OAUTH_PLAYGROUND //https://developers.google.com/oauthplayground
        } = process.env;

        const oauth2Client = new OAuth2(
            MAILING_SERVICE_CLIENT_ID,
            MAILING_SERVICE_CLIENT_SECRET,
            OAUTH_PLAYGROUND
        );

        oauth2Client.setCredentials({
            refresh_token: MAILING_SERVICE_REFRESH_TOKEN,
        });
        //>>> get the accessToken
        const accessToken = oauth2Client.getAccessToken();

        
        let transporter = nodemailer.createTransport({
            service: 'gmail',
            auth: {
                type: 'OAuth2',
                user: SENDER_EMAIL_ADDRESS,
                clientId: MAILING_SERVICE_CLIENT_ID,
                clientSecret: MAILING_SERVICE_CLIENT_SECRET,
                refreshToken: MAILING_SERVICE_REFRESH_TOKEN,
                accessToken,
            },
        });

        let mailOptions = {
            from: 'no-reply@blah.com',
            to: 'to_blah@blah.com',
            subject: 'test',
            text: 'test'
        };
        let result = await transporter.sendMail(mailOptions);
Oguz
  • 1,867
  • 1
  • 17
  • 24