Home > Software engineering >  How do I send email from nodemailer in nodejs using gmail?
How do I send email from nodemailer in nodejs using gmail?

Time:06-17

I followed the documentation but google says the app is less secure than its security level. And there is no option to allow access to such apps anymore.

  const nodemailer = require('nodemailer');


let mailTransporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: '[email protected]',
        pass: '*************'
    }
});

let mailDetails = {
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Test mail',
    text: 'Node.js testing mail for GeeksforGeeks'
};

mailTransporter.sendMail(mailDetails, function(err, data) {
    if(err) {
        console.log('Error Occurs');
    } else {
        console.log('Email sent successfully');
    }
});

CodePudding user response:

At the time of writing, Less Secure Apps is no longer supported by google. And you can't use your google account password.

You're gonna have to generate a new app password.

App passwords only work if 2-step verification is turned on. Follow this steps to get the app password

  1. Go to https://myaccount.google.com/security
  2. Enable 2FA
  3. Create App Password for Email
  4. Copy that password (16 characters) into the pass parameter in Nodemailer auth.
const client = nodemailer.createTransport({
    service: "Gmail",
    auth: {
        user: "[email protected]",
        pass: "Google-App-Password-Without-Spaces"
    }
});

client.sendMail(
    {
        from: "sender",
        to: "recipient",
        subject: "Sending it from Heroku",
        text: "Hey, I'm being sent from the cloud"
    }
)

CodePudding user response:

app is less secure apps is deprecated util 14/06/2022

you need to enable two step auth in your google account https://myaccount.google.com/signinoptions/two-step-verification?rapt=AEjHL4Nm5j8lzlmlfGjIPZ3JQPadURur-daRW6csSgARqTeML2jsYhw3cctrxLoOZXEWpIivj6eXEcaFt_EfQct4VY40OwxOEg

and create App Password https://myaccount.google.com/apppasswords?rapt=AEjHL4PZB2jtGe1EVQ1dS_jyte5bhU_hn44yc3rDR0k3BnmcIqzmocSf5sBDIN88P8vB7-owMYAWLK6m37OyA-_2C6IE7qapTg

so google will send a App password that you can login with nodemailer

  • Related