Nodemailer 不在 aws elastic beanstalk 中发送电子邮件
Posted
技术标签:
【中文标题】Nodemailer 不在 aws elastic beanstalk 中发送电子邮件【英文标题】:Nodemailer does not send emails in aws elastic beanstalk 【发布时间】:2022-01-18 03:21:26 【问题描述】:我在亚马逊网络服务上托管了我的 node js 应用程序,并使用了弹性 beanstalk 服务。我正在使用 nodemailer 发送电子邮件,它在本地运行良好,但是在我将应用程序托管在 aws 中之后,它就停止了发送电子邮件。我尝试解决了这个问题,但没有成功,我也打开了 465 端口,但没有结果。
那么如何解决这个问题,我尝试使用 aws ses 但它不起作用。
这是我得到的错误:
_http_outgoing.js:561
throw new ERR_HTTP_HEADERS_SENT('set');
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (internal/errors.js:322:7)
Dec 14 19:28:14 ip-172-31-18-123 web: at ServerResponse.setHeader (_http_outgoing.js:561:11)
at ServerResponse.header (/var/app/current/node_modules/express/lib/response.js:771:10)
at ServerResponse.send (/var/app/current/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/var/app/current/node_modules/express/lib/response.js:267:15)
at /var/app/current/controllers/user_api.js:116:32
at /var/app/current/node_modules/nodemailer/lib/mailer/index.js:230:21
at /var/app/current/node_modules/nodemailer/lib/smtp-transport/index.js:282:36
at SMTPConnection._actionAUTHComplete (/var/app/current/node_modules/nodemailer/lib/smtp-connection/index.js:1536:20)
at SMTPConnection.<anonymous> (/var/app/current/node_modules/nodemailer/lib/smtp-connection/index.js:540:26)
code: 'ERR_HTTP_HEADERS_SENT'
web:
这是我的代码
const transport = nodeMailer.createTransport(
service: "gmail",
port: 465,
secure: true,
auth:
user: process.env.DORENT_GMAIL,
pass: process.env.DORENT_GMAIL_PASSWORD,
,
);
const mailOption =
from: process.env.DORENT_GMAIL,
to: user.userMail,
subject: "Verifyera mailaddress",
html: `Please click this link to verify your link: <a href=$url>$url</a>`,
;
// console.log(mailOption);
// console.log(transport);
transport.sendMail(mailOption, (err) =>
if (err)
return res.status(500).json( error: "error", message: err.message );
else
console.log("Mail has been sent");
);
【问题讨论】:
尝试查看错误日志。您确定环境变量已正确加载吗?你安装了nodemailer吗?是否存在身份验证错误? 环境变量加载正确 看起来这不是 NodeMailer 的错误。你能列出你所有的文件吗? 【参考方案1】:首先确保您在 Gmail 帐户中启用了不太安全的应用:How to enable less secure apps
然后确保您已启用多因素:How to enable multifactor on your Gmail
然后,您需要生成一个 应用密码,以便在使用 nodemailer 登录时使用。您可以按照本指南进行操作:Generate app password
然后我不得不使用 nodemailer-smpt-transport 和 cors 包来使其在 Firebase 云功能上工作。
我想它可能适用于 AWS:
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const cors = require('cors')(origin: true);
var transport = nodemailer.createTransport(smtpTransport(
service: "gmail",
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth:
user: process.env.DORENT_GMAIL,
pass: GMAIL_APP_PASSWORD,
,
));
cors(req, res, () =>
const mailOption =
from: process.env.DORENT_GMAIL,
to: user.userMail,
subject: "Verifyera mailaddress",
html: `Please click this link to verify your link:`,
;
transport.sendMail(mailOption, (err) =>
if (err)
return res.status(500).json( error: "error", message: err.message );
else
console.log("Mail has been sent");
);
);
【讨论】:
我不认为你使用 CORS 很正确。另外,请注意 OP 尝试在本地成功发送电子邮件,这意味着它可能无法在 Gmail 上配置。以上是关于Nodemailer 不在 aws elastic beanstalk 中发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章