我正在尝试使用Nodemailer发送带有电子邮件的附件,但我收到附件的“意外标识符”错误。似乎nodejs无法识别“附件”。除了nodemaler和path之外,还有什么我必须从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));
});
});
答案 0 :(得分:1)
如果你得到了,这个:
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附件无法正常工作。您的代码有语法错误。