gpt4 book ai didi

node.js - Nodemailer 退出时不会发送邮件

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:30 24 4
gpt4 key购买 nike

在我的脚本中,我想发送一封电子邮件,其中包含有关使用 Nodemailer 运行脚本的数据。脚本中间使用了相同的传输器,我成功发送了电子邮件。然而,退出事件处理程序中的相同代码根本不执行任何操作。没有电子邮件,没有“报告已发送!”输出到控制台,尽管我确实将 mailOption 变量的输出输出到控制台。假设异步操作在退出之前可能尚未完成,我添加了一个临时延迟步骤来验证这一点。仍然没有产生电子邮件。我做错了什么吗?

function exitHandler(er) {
end = moment().utc();

// report template
var tplPath = path.resolve('report.html');
var juiceFileDeAsync = deasync(juice.juiceFile);
var tpl = juiceFileDeAsync(tplPath, { webResources: {images: false} });
var email = nunjucks.renderString(tpl, {
startDate: start.format("MMMM Do YYYY, h:mm:ss a"),
duration: end.diff(start,'seconds'),
report: sentEmails,
warns: warns,
errors: errors
});
// email report
mailOptions.to = '<valid email>';
mailOptions.html = email;
transporter.sendMail(mailOptions, function(error, info) {
console.log('Report sent!', error, info);
});
console.log(mailOptions);
if (er) {
console.log(er);
}

var e = new Date().getTime() + (10 * 1000);
while (new Date().getTime() <= e) {
;
}

}

// do something when app is closing
process.on('exit', exitHandler);
process.on('uncaughtException', exitHandler);

最佳答案

https://nodejs.org/api/process.html看一下事件:退出文档:

process.on('exit', function(code) {
// do *NOT* do this
setTimeout(function() {
console.log('This will not run');
}, 0);
console.log('About to exit with code:', code);
});

once all exit listeners have finished running the process will exit. Therefore you must only perform synchronous operations in this handler.

关于node.js - Nodemailer 退出时不会发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com