I am wanting to attach a PDF document using nodemailer and node.js, however, the only examples I am finding for attachments with nodemailer is with .txt files (here).
我想使用nodemailer和node.js附加PDF文档,但是,我找到的与nodemailer附件的唯一示例是.txt文件(这里)。
Does anyone know if PDF document attachment is supported with nodemailer?
有没有人知道nodemailer是否支持PDF文档附件?
Initially it appears a PDF can be attached, yet the PDF file that arrives through the email appears to be damaged (see image).
最初看起来可以附加PDF,但通过电子邮件到达的PDF文件似乎已损坏(见图)。
Code: (Adapted from Mahesh's answer)
代码:(改编自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);
});
Terminal Response:
<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
1 个解决方案
#1
10
Yes you can. You have to add the path of the file which you are trying to attach.
是的你可以。您必须添加要尝试附加的文件的路径。
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);
res.send(err);
}
else{
console.log(info);
res.send(info);
}
});
#1
10
Yes you can. You have to add the path of the file which you are trying to attach.
是的你可以。您必须添加要尝试附加的文件的路径。
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);
res.send(err);
}
else{
console.log(info);
res.send(info);
}
});