Node.js和Nodemailer:我们可以将PDF文档附加到电子邮件中吗?

Val*_*Val 8 javascript email-attachments node.js nodemailer

我想使用nodemailer和node.js附加PDF文档,但是,我找到的与nodemailer附件的唯一示例是.txt文件(这里).

有谁知道节点制作者是否支持PDF文档附件?

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

在此输入图像描述

代码:(改编自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);
});
Run Code Online (Sandbox Code Playgroud)

终端响应:

<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
Run Code Online (Sandbox Code Playgroud)

Vis*_*hnu 15

是的你可以.您必须添加要尝试附加的文件的路径.

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);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 在内存中生成 pdf 时,我不能这样做(即没有文件)。任何地方都没有示例(base64 编码图像除外)。 (3认同)

Mr.*_*ang 9

是的,您可以将 pdf 文档附加到电子邮件中,对于路径,您可以使用 path 并遵循从当前文件到要附加的 pdf 的路径。这对我有用。

\n\n
// node-mailer.js\nconst nodemailer = require('nodemailer');\nconst path = require('path');\n...\nconst mailOptions = {\n    from: '',\n    to: '',\n    subject: '',\n    text: '',\n    html: '',\n    attachments: [\n        {\n            filename: 'file-name.pdf', // <= Here: made sure file name match\n            path: path.join(__dirname, '../output/file-name.pdf'), // <= Here\n            contentType: 'application/pdf'\n        }\n    ]\n};\n\ntransporter.sendMail(mailOptions, function(error, cb) {\n\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  • 我的文件结构/树:
  • \n
\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package-lock.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file-name.pdf\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 routes\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 node-mailer.js <= Current file\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 server.js\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

希望这对某人有帮助:) \n快乐编码!

\n