简体   繁体   English

Nodemailer、Heroku、Gmail、无效登录 - 在本地工作

[英]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.过去几周我一直遇到这个问题,它每次都在本地工作,但是一旦我将它部署到我的 heroku 服务器,它就会给我一个invalid login错误。 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.并且凭据是正确的,并且每次都在 localhost 上运行。 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).您可以使用此链接允许计算机远程访问您的 gmail,但请记住,Google 会影响您的默认帐户(即使您已连接到另一个帐户)。

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.简单的方法:使用incognito / private窗口(没有连接谷歌帐户)并登录您的谷歌帐户,然后使用上面的链接。

If that doesn't work try updating your nodemailer to the latest version.如果这不起作用,请尝试将您的 nodemailer 更新到最新版本。

According to https://support.google.com/accounts/answer/6009563 , Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha and allow access and try again ..根据https://support.google.com/accounts/answer/6009563 ,转到https://accounts.google.com/b/0/DisplayUnlockCaptcha并允许访问并重试..

Also according to docs at nodemailer ( https://github.com/nodemailer/nodemailer#delivering-bulk-mail )同样根据 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.我在使用 Gmail 时遇到问题 Gmail 要么运行良好,要么根本无法运行。 It is probably easier to switch to an alternative service instead of fixing issues with Gmail.切换到替代服务可能比解决 Gmail 问题更容易。 If Gmail does not work for you then don't use it.如果 Gmail 不适合您,请不要使用它。

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.我为这个问题设置了悬赏,但在浏览了 Nodemailer 文档/Github 之后,似乎使用 Gmail 只会导致头痛。

According to the Nodemailer docs ...根据Nodemailer 文档...

"...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. “...Gmail 期望用户是实际用户而不是机器人,因此它对每次登录尝试运行大量启发式方法,并阻止任何看起来可疑的内容以保护用户免受帐户劫持企图。

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.我的 Nodemailer 在我的机器上运行没有问题,但是当我将应用程序推送到 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 .我已经尝试了 Google 提供的所有允许使用该帐户的方法,并且我完全相信Nodemailer 和 Gmail根本不可能获得一致的结果

Using Outlook/Hotmail worked for me.使用 Outlook/Hotmail 对我有用。 I also tried switching to a different Gmail account.我还尝试切换到其他 Gmail 帐户。 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.大多数情况下,当 Nodemailer 出现这种错误时,Google 列出的这些选项之一可以修复它,花点时间检查要使用的 google 帐户是否具有所需的设置。

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第 1 步:检查您的密码

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第 2 步:尝试这些故障排除步骤

  • 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 .通过https://mail.google.com的网络版 Gmail 登录您的帐户。 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.访问显示解锁验证码并使用您的 Gmail 用户名和密码登录。 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.如果您的邮件应用程序每 10 分钟检查一次以上的新邮件,则该应用程序对您帐户的访问可能会被阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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