Node.js nodeEmailer附件不';行不通
我正在尝试使用Nodemailer通过电子邮件发送附件,但我收到附件的“意外标识符”错误。似乎nodejs不认识“附件”。除了nodemaler和path之外,我还需要从npm安装其他东西吗 以下是电子邮件发送路线:Node.js nodeEmailer附件不';行不通,node.js,express,Node.js,Express,我正在尝试使用Nodemailer通过电子邮件发送附件,但我收到附件的“意外标识符”错误。似乎nodejs不认识“附件”。除了nodemaler和path之外,我还需要从npm安装其他东西吗 以下是电子邮件发送路线: app.post("/send", function(req,res){ var transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'my gamil',
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));
});
});
app.post(“/send”),函数(req,res){
var transporter=nodeEmailer.createTransport({
服务:“gmail”,
认证:{
用户:“我的gamil”,
密码:“我的gmail密码”
}
});
常量邮件选项={
发件人:req.body.fr,//发件人地址
收件人:req.body.to,//收件人列表
密件抄送:req.body.fr,
主题:req.body.subject,//主题行
html:'亲爱的'+req.body.contname+'+''+req.body.message+''+'亲切问候'+req.body.user//html body
附件:[
{
文件路径:req.body.myFile,
},
{
文件名:req.body.myFile,
},
],
};
transporter.sendMail(邮件选项,(错误,信息)=>{
如果(错误){
返回console.log(错误);
}
console.log('发送的消息:%s',info.messageId);
log('预览URL:%s',nodeEmailer.getTestMessageUrl(信息));
});
});
如果您得到:
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
}]
};
const mailpoptions={
发件人:req.body.fr,//发件人地址
收件人:req.body.to,//收件人列表
密件抄送:req.body.fr,
主题:req.body.subject,//主题行
//html结尾缺少逗号=>
html:'亲爱的'+req.body.contname+'+''+req.body.message+''+'亲切问候'+req.body.user,//此处缺少逗号
附件:[{
文件路径:req.body.myFile
} {
文件名:req.body.myFile
}]
};
它与nodeEmailer附件不工作无关。你的代码有语法错误。记住,
SyntaxError
是关键:)现在我可以发送附件了,但是当我收到电子邮件时,无论是jpg还是pdf,附件都不可读,你知道问题所在吗?是的,你没有发送实际的文件,什么是:console.log(req.body.myFile)
打印的?文件名?它打印filename.pdf。我不知道这是一个真实的文件还是一个文件名!!它没有指向,它是一个包含文件名的字符串,这就是它不起作用的原因。你用什么上传文件?穆特?