node.js - Nodemailer 附件不起作用
问题描述
我正在尝试使用 Nodemailer 发送带有电子邮件的附件,但附件出现“意外标识符”错误。似乎 nodejs 无法识别“附件”。除了nodemaler之外,我还需要从npm安装什么,下面的路径是电子邮件路由:
app.post("/send", function(req,res){
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'my gamil',
pass: 'my gmail password'
}
});
const mailOptions = {
from: req.body.fr, // sender address
to: req.body.to, // list of receivers
bcc: req.body.fr,
subject: req.body.subject, // Subject line
html: '<h4>Dear ' + req.body.contname+ '</h4>' + '<p>'+ req.body.message + '</p>' + '<p>Kind Regards</p>' + req.body.user// html body
attachments: [
{
filePath: req.body.myFile,
},
{
filename: req.body.myFile,
},
],
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
});
解决方案
如果你得到,这个:
attachments: [{
^^^^^^^^^^^
SyntaxError: Unexpected identifier
这是因为您在html
属性末尾缺少逗号。
const mailOptions = {
from: req.body.fr, // sender address
to: req.body.to, // list of receivers
bcc: req.body.fr,
subject: req.body.subject, // Subject line
// Comma missing at the end of html =>
html: '<h4>Dear ' + req.body.contname + '</h4>' + '<p>' + req.body.message + '</p>' + '<p>Kind Regards</p>' + req.body.user, // Comma missing here
attachments: [{
filePath: req.body.myFile
} {
filename: req.body.myFile
}]
};
它与 nodemailer 附件不起作用无关。您的代码有语法错误。
推荐阅读
- keras - Keras Fixing Seed 对训练数据改组的影响
- javascript - 为什么onload会导致我的图像切换闪烁?
- .net - 用vb.Net创建BitmapDecoder和SoftwareBitmap的方法是什么
- javascript - 在 JavaScript 中使用合并排序对表列进行排序
- java - FileReaders readLine 总是返回 null JAVA
- c# - 在 ASP.NET Core 控制器中模拟 HttpRequest
- ios - 如何使用目标 c 中的本地通知在特定时间每天调用 Web 服务
- angular - Angular 编译器和 tsconfig.json
- html - 未显示 Angular 4 按钮禁用图标
- javascript - 当 jquery 动态添加元素时,外部 java 脚本“mtree 脚本”不起作用