Nodemailer with Gmail service not working on heroku

22,700

Solution 1

I believe this is an issue with google account security.

  • Google blocked your sign-in to use the mailing features due to an unknown device (location).

A few step to verify this:

  • Start your server locally and sends the email.

  • Check your account alerts for unknown sign-in.

This can be temporally resolved by: https://accounts.google.com/DisplayUnlockCaptcha

A more permanent resolution would be to change your password to a stronger level:

upper case letter + lower case letter + special symbols + numbers

Solution 2

Instead of using direct gmail credentials like this

auth: {
    user: '[email protected]',
    pass: 'foobar'
}

Use OAuth2

 auth: {
    type: 'OAuth2',
    user: '[email protected]',
    accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
}

Google blocks the heroku IPs (unsafe), if we are using direct credentials like you mentioned above. You can refer this Medium article here

Solution 3

5 years later, I still struggled this problem (all answers found on SO failed in my case). Here is what I did, for now everything works smoothly:

enter image description here

enter image description here

  • Configure NodeMailer like so:

      const nodemailer = require('nodemailer'),
          sgTransport = require('nodemailer-sendgrid-transport');
    
      const mailTransporter = nodemailer.createTransport(sgTransport({
          auth: {
              api_key: process.env.ADMIN_EMAIL_API_KEY // your api key here, better hide it in env vars
          }
      }))
    
  • To send an email now, you have to add your gmail in 'Reply To' field, like so:

      mailTransporter.sendMail({
          from: `"Admin" <[email protected]>`,
          to: '[email protected]',
          replyTo: '[email protected]',
          subject: 'Something',
          html: `Boom !`
      });
    

I think that's all, in case I forgot something, please add a comment below

Share:
22,700
skip
Author by

skip

Updated on May 26, 2021

Comments

  • skip
    skip almost 3 years

    I've got a basic email setup done for sending an email using Nodemailer with AngularJS and NodeJS and I've got the project deployed on heroku.

    The emailing seems to be working just fine when I am running the app on heroku, but when I get it deployed to Heroku no emails are sent.

    For authentication, I am using a Gmail address and I also have a bcc to another Gmail address. So from and bcc addresses are two different Gmail addresses. The from address is the same as the address used for authentication.

    Could somebody help me with resolving this issue?

    Edit: Adding code

    var nodemailer = require('nodemailer');
    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            user: '[email protected]',
            pass: 'foobar'
        }
    });
    
    router.post('/send',function(req,res){
    
        var mailOptions = {
            from: 'Foo Bar ✔ <[email protected]>',
            to: req.body.email,
            subject: "Hello " + req.body.email,
            text: 'Hello ' + req.body.email + '✔',
            html: "<p>Hello " + req.body.email + " </p>",
            bcc: "[email protected]"
        };
        transporter.sendMail(mailOptions, function(error, info){
            if(error){
                console.log(error);
            }else{
                console.log('Message sent: ' + info.response);
                res.send(200);
            }
        });        
    });
    
    • Peter Lyons
      Peter Lyons over 9 years
      Please include the error message.
    • skip
      skip over 9 years
      @PeterLyons: Error: Invalid login, responseCode: 534, code: 'EAUTH'.
    • Peter Lyons
      Peter Lyons over 9 years
      Well, got any theories? Seems pretty clearly wrong credentials to me.
    • skip
      skip over 9 years
      @PeterLyons: Looking forward to hear one from you as I could get the app working when testing locally. Am just wondering if its a captcha issue that gmail might be asking me to use when using my gmail credentials through heroku. So may be turning that off might make gmail happy?
    • skip
      skip over 9 years
      @PeterLyons: Yep, looked like I had to go here accounts.google.com/DisplayUnlockCaptcha and turn off this security precaution off from my gmail account for allowing the machine I've got my application deployed on through heroku to be able to send the emails through my gmail account using nodemailer. Good stuff by gmail.
    • Peter Lyons
      Peter Lyons over 9 years
      Cool. See the nodemailer warning about trying to send bulk mail with services not explicitly intended for that. Consider switching to a real service designed for transactional email from applications such as mailgun, postmarkapp, sendgrid, amazon SES, etc.
    • Valeed Anjum
      Valeed Anjum over 2 years
      accounts.google.com/b/0/DisplayUnlockCaptcha allowing login access to new devices or machines works fine but for a short period of time.
  • Luc
    Luc over 3 years
    that made it for me. seems like heroku is sometimes blocked from gmail. help.heroku.com/CFG547YP/…
  • David George Smith
    David George Smith over 3 years
    The google playground has changed a bit, but the article still has everything you need to get up and running. Thanks
  • Utkarsh
    Utkarsh over 2 years
    One suggestion: Use 'sender' email address in 'from'. app.sendgrid.com/settings/sender_auth/senders
  • Tammibriggs
    Tammibriggs over 2 years
    changing the password worked for me. Thanks
  • Saad Abbasi
    Saad Abbasi about 2 years
    I was not receiving emails with Gmail Service i have tried different things but changing the password did work for me :-)