繁体   English   中英

Gmail配Heroku发邮件

[英]Gmail with Heroku to send mail

我想在我的 web 应用程序中添加订单后我正在发送邮件。

我选择 Nodemailer 是因为它是最著名的 npm 使用。

我对我的请求进行了编码,并且在本地环境中,它正在工作。

我将代码上传到 Heroku 并收到错误消息。

错误:无效登录:534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs

我检查了人们告诉我要禁用我在这里所做的验证码: UnlockCaptcha

现在我仍然收到同样的错误,我收到一封邮件说谷歌阻止了连接我该怎么办?

 const nodemailer = require('nodemailer'); const { sendLog } = require('../middleware/sendLog'); const { coupons, actions } = require('../constant/actionCoupon'); var simple = function () { var textMultiple = { text1: 'text1', text2: 'text2', }; return textMultiple; }; // send mail system for the (REQUEST ACCEPTED SYSTEM) const sendMail = (mail, action) => { let mailTransporter = nodemailer.createTransport({ service: 'gmail', auth: { user: process.env.MAIL, pass: process.env.PASSWORD, }, }); let mailDetails = { from: process.env.MAIL, to: mail, subject: `Thank you for your purchase. with love FameGoal`, text: "for any probleme please reply on this message", }; mailTransporter.sendMail(mailDetails, function (err, data) { if (err) { console.log(err); console.log(`error sent mail to ${mail}`, 'error'); } else { console.log('succeed'); console.log(`succesfully sent mail to ${mail}`, 'info'); } }); }; exports.sendMail = sendMail;

使用 Gmail 作为 SMTP 中继并不是最理想的,因为 Google 服务器有时可能会拒绝基本的用户名/密码身份验证。

有一些解决方法。 最理想的是使用 OAuth2 发送电子邮件。

OAuth2

OAuth2 使用访问令牌而不是密码来执行身份验证。 我不会 go 设置 OAuth2 的步骤,因为它可能需要一些时间,但如果你有兴趣,这个答案: https://stackoverflow.com/a/51933602/10237430会遍历所有步骤。

应用密码

如果您尝试发送电子邮件的 Google 帐户启用了两步验证,则无法使用密码发送电子邮件。 相反,您需要在 Google 网站上生成一个应用专用密码并将其传递到密码字段中。

更多信息: https://support.google.com/accounts/answer/185833?hl=en

启用不太安全的应用程序

如果您仍想使用当前设置,则必须确保在您发送电子邮件的 Google 帐户上启用安全性较低的应用程序。 这将让您仅使用 email 和密码向 Google 进行身份验证。

更多信息: https://support.google.com/accounts/answer/6010255?hl=en

除非您启用安全性较低的应用程序,否则基本密码身份验证将不起作用

暂无
暂无

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

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