javascript Node.js 和 Nodemailer:我们可以将 PDF 文档附加到电子邮件中吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32730631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js and Nodemailer: Can we attached PDF documents to emails?
提问by Val
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 <[email protected]>', // sender address
to: 'toPersonName <[email protected]>', // 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
回答by Vishnu
Yes you can. You have to add the path of the file which you are trying to attach.
是的你可以。您必须添加要附加的文件的路径。
transporter.sendMail({
from: '[email protected]',
to: '[email protected]',
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);
}
}
});
回答by Mr. Dang
Yes you can attach pdf documents to the email, for the path you could use path and follow from your current file to the pdf you'd like to attach. This works for me.
是的,您可以将 pdf 文档附加到电子邮件中,对于您可以使用的路径,从当前文件到要附加的 pdf。这对我有用。
// node-mailer.js
const nodemailer = require('nodemailer');
const path = require('path');
...
const mailOptions = {
from: '',
to: '',
subject: '',
text: '',
html: '',
attachments: [
{
filename: 'file-name.pdf', // <= Here: made sure file name match
path: path.join(__dirname, '../output/file-name.pdf'), // <= Here
contentType: 'application/pdf'
}
]
};
transporter.sendMail(mailOptions, function(error, cb) {
});
- My files structure/tree:
- 我的文件结构/树:
├── package-lock.json
├── package.json
├── output
│ └── file-name.pdf
├── routes
│ └── node-mailer.js <= Current file
└── server.js
Hope this helps someone :) Happy Coding!
希望这对某人有所帮助:) 快乐编码!