【问题标题】:Nodemailer Gmail SMTP not working on herokuNodemailer Gmail SMTP在heroku上不起作用
【发布时间】:2021-11-28 01:51:30
【问题描述】:

我正在使用nodemailer 发送邮件
它在本地实例上完美运行,因为它是同一设备
注意:这只是邮件程序的表示,实际代码库中有不同的值
我的代码
结节.js

const env = require("dotenv").config();
const nodemailer = require("nodemailer");
module.exports = {
  email: (html) => {
    var mail = nodemailer.createTransport({
      service: "gmail",
      auth: {
        user: "notanactualmail@gmail.com",
        pass: process.env.DEMO_ACCOUNT_PASSWORD
      }
    });

    var mailOptions = {
      from: "notanactualmail@gmail.com",
      to: process.env.TARGET_MAIL_ID,
      subject:
        "my title",
      html: html
    };

    mail.sendMail(mailOptions, (error, info) => {
      if (error) {
        console.log(error);
      }
    });
  }
}

app.js

const rh = require(`./module.js`);
rh.email("<h1>Hello World</h1>");

问题

所以重点是每天谷歌都会说Unauthorised login preventedSuspicious activity等等...等等...
Heroku 日志

2021-10-08T03:24:30.383820+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-10-08T03:24:31.277868+00:00 app[web.1]: app is listening on port 19616
2021-10-08T03:24:32.059451+00:00 heroku[web.1]: State changed from starting to up
2021-10-08T03:24:32.726090+00:00 heroku[router]: at=info method=GET path="/autotrg/ifttt/auth/<Hardcoded name>" host=myapp.herokuapp.com request_id=0032f1f2-7641-475c-85eb-70e1d7d94fe0 fwd="45.248.22.6" dyno=web.1 connect=0ms service=6ms status=304 bytes=149 protocol=https
2021-10-08T03:24:34.224740+00:00 app[web.1]: Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt
2021-10-08T03:24:34.224757+00:00 app[web.1]: 534-5.7.14 suR76UfS5DkBscwE1tOwUYwx2Z6T5MR13HZ8QXHGtOMTfYBSdNVVOvhVItO5_3XwC-NYO
2021-10-08T03:24:34.224758+00:00 app[web.1]: 534-5.7.14 gerVJfsXtIw9zM5OALS3cng_HqJZOQRp5pr1ZyB7xPF7kbXcX8whhc2BDOtoMxsC>
2021-10-08T03:24:34.224758+00:00 app[web.1]: 534-5.7.14 Please log in via your web browser and then try again.
2021-10-08T03:24:34.224759+00:00 app[web.1]: 534-5.7.14  Learn more at
2021-10-08T03:24:34.224759+00:00 app[web.1]: 534 5.7.14  https://support.google.com/mail/answer/78754 l19sm1065983qkk.12 - gsmtp
2021-10-08T03:24:34.224759+00:00 app[web.1]:     at SMTPConnection._formatError (/app/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
2021-10-08T03:24:34.224759+00:00 app[web.1]:     at SMTPConnection._actionAUTHComplete (/app/node_modules/nodemailer/lib/smtp-connection/index.js:1536:34)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at SMTPConnection.<anonymous> (/app/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at SMTPConnection._processResponse (/app/node_modules/nodemailer/lib/smtp-connection/index.js:947:20)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at SMTPConnection._onData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
2021-10-08T03:24:34.224760+00:00 app[web.1]:     at TLSSocket.SMTPConnection._onSocketData (/app/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
2021-10-08T03:24:34.224761+00:00 app[web.1]:     at TLSSocket.emit (events.js:400:28)
2021-10-08T03:24:34.224761+00:00 app[web.1]:     at addChunk (internal/streams/readable.js:293:12)
2021-10-08T03:24:34.224762+00:00 app[web.1]:     at readableAddChunk (internal/streams/readable.js:267:9)
2021-10-08T03:24:34.224762+00:00 app[web.1]:     at TLSSocket.Readable.push (internal/streams/readable.js:206:10) {
2021-10-08T03:24:34.224762+00:00 app[web.1]:   code: 'EAUTH',
2021-10-08T03:24:34.224762+00:00 app[web.1]:   response: '534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt\n' +
2021-10-08T03:24:34.224762+00:00 app[web.1]:     '534-5.7.14 suR76UfS5DkBscwE1tOwUYwx2Z6T5MR13HZ8QXHGtOMTfYBSdNVVOvhVItO5_3XwC-NYO\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534-5.7.14 gerVJfsXtIw9zM5OALS3cng_HqJZOQRp5pr1ZyB7xPF7kbXcX8whhc2BDOtoMxsC>\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534-5.7.14 Please log in via your web browser and then try again.\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534-5.7.14  Learn more at\n' +
2021-10-08T03:24:34.224763+00:00 app[web.1]:     '534 5.7.14  https://support.google.com/mail/answer/78754 l19sm1065983qkk.12 - gsmtp',
2021-10-08T03:24:34.224764+00:00 app[web.1]:   responseCode: 534,
2021-10-08T03:24:34.224764+00:00 app[web.1]:   command: 'AUTH PLAIN'
2021-10-08T03:24:34.224764+00:00 app[web.1]: }

我尝试过的

我错过了什么?

【问题讨论】:

  • Gmail 的设计初衷不是作为程序化使用的邮件中继。我强烈建议您使用其他服务,例如 SendGrid 或 Mailgun。两者都有免费等级,两者都会让您少得多的麻烦。例如,请参阅 Laravel password reset email not sending using gmail on Heroku 和 My Node.js not sending mails after uploading it to Heroku。
  • oauth 2.0 会正常工作,因为我认为注册这些服务是多余的,如果是,请提供一些代码示例
  • ...OAuth 与什么有什么关系?而且我们不是来“给你代码示例”的。请拨打tour。
  • dosen't oauth 提供对您帐户的访问权限,例如我授予我的应用程序访问权限以代表我写电子邮件like so
  • 你把事情混为一谈了。这是为了访问您的用户的 Gmail 帐户。

标签: node.js heroku gmail nodemailer


【解决方案1】:

我找到了解决方案
它使用 oauth 2.0 和 googleapis
但是你仍然受到 api 配额的限制

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-09
    • 2018-01-06
    • 2023-04-03
    • 2011-06-17
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多