gpt4 book ai didi

javascript - Node.js 和 Nodemailer : Can we attached PDF documents to emails?

转载 作者:数据小太阳 更新时间:2023-10-29 04:05:59 29 4
gpt4 key购买 nike

我想使用 nodemailer 和 node.js 附加一个 PDF 文档,但是,我找到的关于 nodemailer 附件的唯一示例是 .txt 文件 ( here )。

有谁知道 nodemailer 是否支持 PDF 文档附件?

最初似乎可以附加 PDF,但通过电子邮件到达的 PDF 文件似乎已损坏(见图)。

enter image description here

代码:(改编自 Mahesh 的回答)

fs.readFile('/filePath/fileName.pdf', function (err, data) {
if (err) throw err;
var mailOptions = {
from: 'Test <formEmail@gmail.com>', // sender address
to: 'toPersonName <toEmail@gmail.com>', // list of receivers
subject: 'Attachment', // Subject line
text: 'Hello world attachment test', // plaintext body
html: '<b>Hello world attachment test HTML</b>', // html body
attachments: [
{
filename: 'fileName.pdf',
contentType: 'application/pdf'
}]
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
console.log(data);
});

终端响应:

<Buffer 25 50 44 46 2d 31 2e 33 0a 25 c4 e5 f2 e5 eb a7 f3 a0 d0 c4 c6 0a 34 20 30 20 6f 62 6a 0a 3c 3c 20 2f 4c 65 6e 67 74 68 20 35 20 30 20 52 20 2f 46 69 ... >
Message sent: 250 2.0.0 OK 1443026036 hq8sm3016566pad.35 - gsmtp

最佳答案

是的,你可以。您必须添加要附加的文件的路径。

transporter.sendMail({
from: 'foo@bar.com',
to: 'bar@foo.com',
subject: 'An Attached File',
text: 'Check out this attached pdf file',
attachments: [{
filename: 'file.pdf',
path: 'C:/Users/Username/Desktop/somefile.pdf',
contentType: 'application/pdf'
}],
function(err, info) {
if (err) {
console.error(err);
} else {
console.log(info);
}
}
});

关于javascript - Node.js 和 Nodemailer : Can we attached PDF documents to emails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32730631/

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