Nodemailer with Gmail service not working on heroku Nodemailer with Gmail service not working on heroku heroku heroku

Nodemailer with Gmail service not working on heroku


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


Instead of using direct gmail credentials like this

auth: {    user: 'foobar@gmail.com',    pass: 'foobar'}

Use OAuth2

 auth: {    type: 'OAuth2',    user: 'user@example.com',    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


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" <any_address@heroku.com>`,      to: 'receiver@hotmail.com',      replyTo: 'your_gmail@gmail.com',      subject: 'Something',      html: `Boom !`  });

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