Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

receive mail with nodemailer without setting "Allow less secure apps to access"

Tags:

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

Var client = nodemailer.createTransport ({ 
Service: 'gmail',
Auth: {
User: '[email protected]', // 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 [email protected] TO [email protected], without setting "Allow less secure apps to access" and message directly accept in email box !!??? Or any other plugin that should be added ??

THANX;)

like image 902
Adriyana Putra Pratama Avatar asked Aug 12 '17 17:08

Adriyana Putra Pratama


People also ask

Does Gmail work for Nodemailer?

To get Gmail working with nodemailer, most times, all you have to do is configure your google account by allowing access to "less secure apps" from the security. With this enabled, you could use your Google email address and password for nodemailer and start sending emails right away.

What is Nodemailer SMTP transport?

SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so its truly universal. Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending.


1 Answers

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:'[email protected]',
      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: '[email protected]', 
  to: '[email protected]', 
  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;
like image 102
vikasThakur.com Avatar answered Oct 11 '22 14:10

vikasThakur.com