gpt4 book ai didi

Node.js 和 Express : attachment on nodemailer

转载 作者:太空宇宙 更新时间:2023-11-04 02:12:00 24 4
gpt4 key购买 nike

我想使用 Nodemailer 在电子邮件中附加文件。我按照本指南尝试了多种方法:https://nodemailer.com/using-attachments/ 、.pdf、.txt...但没有任何效果。有任何想法吗?这是我最后一次尝试的代码:

router.post('/profilocliente/:id', function(req, res, next) {
Customer.findById(req.params.id, function(err, customer){
if(err) throw err;
var mailOptions={
to : customer.email,
subject : req.body.oggetto,
text : req.body.testo,
attachments: [{path: 'C:/Users/Angelo/Desktop/test.txt'}]
}
console.log(mailOptions);
smptTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.end("error");
}else{
console.log("Message sent: " + response.message);
res.redirect('/clienti/profilocliente/'+customer._id);
}
})
})
});

最佳答案

我有同样的问题,不知道为什么无法从桌面发送文件。把它放在D盘和项目目录外面,下面是我的代码

exports.sendmail=function (to,subject,text,html,info) {


var mailOptions = {
attachments: [

{ // file on disk as an attachment
filename: 'ab.txt',
path: 'D:/ab.txt' // stream this file
//below one not working some time dont know why but its work for me now
path: 'C:/Users/Me/Desktop/ab.txt'
//this also work foo me
path: 'D:/myproject/files/ab.txt'
}],
from: '"Testing Mail" <sender@gmail.com>', // sender address
to: to, // list of receivers
subject: subject, // Subject line
text: text, // plaintext body
html: html // html body
};
transporter.sendMail(mailOptions, function(error, infoo){
var res={
responsecode:''
}
if(error){
res.responsecode=401;
return info(new Error(res.responsecode),null);
}
res.responsecode=200;
info(null,res.responsecode+infoo.response);
});}

enter image description here

关于Node.js 和 Express : attachment on nodemailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693325/

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