简体   繁体   中英

Nodemailer, Heroku, Gmail, invalid login - works locally

I have been having this issue for the past couple of weeks, it works every time locally, however once I deploy it to my heroku server, it will give me an invalid login error. I have gone into the account and givin access to less secure apps. And the credentials are correct, and it works on localhost every time. Is there something I am missing?

quickSendMail: function(routeBody, callback) {
//configuring the nodemailer for email notifications
  var smtpConfig = {
          host: 'smtp.gmail.com',
          port: 465,
          secure: true, // use SSL
          auth: {
              user: 'mysmtpemail123',
              pass: '******'
            }
        };

  var mailOptions = {
          from: 'SageStorm Site <mysmtpemail123@gmail.com>',
          to: ['my email'],
          subject: routeBody.subject,
          html: 'my message'
        };

  var transporter = nodemailer.createTransport(smtpConfig);

  transporter.verify(function(error, success) {
        if (error) {
                console.log(error);
        } else {
                console.log('server is ready to send emails');
          }
        })
  transporter.sendMail(mailOptions, function(error, info) {
        if (error) {
              console.log(error);
              return callback(err, null);
        } else {
              console.log('Message sent: ' + info.response);
              return callback(null, info);
          }
        })
    }

You can allow machines to access your gmail remotely using this link , but keep in mind that Google will affect your default account (even if you are connected with another one).

The easy way: use a incognito / private window (to have no google account connected) and log in your google account and then use the link above.

If that doesn't work try updating your nodemailer to the latest version.

According to https://support.google.com/accounts/answer/6009563 , Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha and allow access and try again ..

Also according to docs at nodemailer ( https://github.com/nodemailer/nodemailer#delivering-bulk-mail )

I'm having issues with Gmail Gmail either works well or it does not work at all. It is probably easier to switch to an alternative service instead of fixing issues with Gmail. If Gmail does not work for you then don't use it.

I set a bounty to this question, but after looking through the Nodemailer docs/Github, it seems that using Gmail will only lead to headaches.

According to the Nodemailer docs ...

"...Gmail expects the user to be an actual user not a robot so it runs a lot of heuristics for every login attempt and blocks anything that looks suspicious to defend the user from account hijacking attempts.

For example you might run into trouble if your server is in another geographical location – everything works in your dev machine but messages are blocked in production."

I believe the second thing was my issue; my Nodemailer ran without issue on my machine, but immediately failed when I pushed the app to Heroku.

As for a fix, my only fix was to use a different email provider. All email accounts I've tried using have been fully authorized to allow other sign-ins and less safe apps; I've gone through every method Google has to offer to allow usage of the account, and I fully believe that it's simply not possible to get consistent results with Nodemailer and Gmail .

Using Outlook/Hotmail worked for me. I also tried switching to a different Gmail account. It worked successfully a couple times, then returned to the previous state.

Most times when one ends up with this kinda error from Nodemailer, one of these options listed by Google gets to fix it, take your time to go through the google account to be used has the required setting.

Google has listed all the potential problems and fixes for us. In as much as you turned on less secure apps setting . Be sure you are applying these to the correct account.

Step 1: Check your password

If you have these problems or can't sign in, first check to make sure you're using the right password.

Step 2: Try these troubleshooting steps

  • If you've turned on 2-Step Verification for your account, you might need to enter an App password instead of your regular password.
  • Sign in to your account from the web version of Gmail at https://mail.google.com . Once you're signed in, try signing in to the mail app again.
  • Visit Display Unlock Captcha and sign in with your Gmail username and password. If asked, enter the letters in the distorted picture.
  • Your app might not support the latest security standards. Try changing a few settings to allow less secure apps access to your account.
  • Make sure your mail app isn't set to check for new email too often. If your mail app checks for new messages more than once every 10 minutes, the app's access to your account could be blocked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM