【发布时间】:2022-01-06 19:16:35
【问题描述】:
我有一个奇怪的问题。我在 node.js 中做了一个小应用程序,每天使用 nodemailer 发送电子邮件。 当我从终端运行它(用于测试)时,它运行良好。 但是当我向 crontab 添加作业时,会发送电子邮件,但里面缺少一些内容。
这是我的运输机配置:
// send mail with defined transport object
let info = await transporter.sendMail({
priority: 'high',
from: '"Example" <example@gmail.com>', // sender address
to: 'recip@gmail.com', // list of receivers
subject: 'Example title: '+currentDate, // Subject line
text: '', // plain text body
html: header + missing + carrierStatArr + ending,// html body
attachments: attachments
});
以及html字段的变量代码:
let carrierStatArr = [], attachments = [];
let header = `<h1>some text ${currentDate}</h1><div><i>some text</br>some text</i></div>`;
let missing = `<h3 style="color:red;">some text: <b>${missingArr}</b></h3>`;
for (let i in checkResultArr) {
let imgName = checkResultArr[i].file;
let correctedImgName = imgName.substring(0,16);
carrierStatArr.push(`<p>Some text <b>${checkResultArr[i].name}</b>,</br>
<span>Some text: <b>${checkResultArr[i].status}</b></span></br>
<span><img src="cid:${correctedImgName}" width="1000px"/></span>
</p>`);
}
//console.log(carrierStatArr);
attachments = checkResultArr.map((file)=>{
let pat = '/var/www/html/public_html/MM1_service/images/';
let fi = file.file;
let fit = fi.substring(0,16);
return {path: pat+file.file, cid: fit};
});
//console.log(attachments[0].path);
let ending = `<hr size="1" width="100%" color="gray"><p><i>some text</i></p>`;
当然,数组中的所有数据。正如我所写,当我使用终端 /node sendmail.js/ 手动运行此代码时,它运行良好,电子邮件包含所有信息、文本和图像。 但是当脚本由 cron 作业自动运行时,电子邮件只有标题、缺失和结束变量(并且数组中的内容在那里),但其余的:carrierStatArr,附件丢失。 这是为什么?手动工作,不完全由 cron 运行。
【问题讨论】:
标签: node.js cron nodemailer