NodeMailer附件不使用smtp/gmail发送

问题描述:

我使用NodeMailer创建了以下函数,它似乎在没有问题的情况下发送电子邮件(控制台和收到的电子邮件中的“发送消息”通知),但没有发送任何电子邮件的附件!NodeMailer附件不使用smtp/gmail发送

试了一堆电子邮件地址(Gmail,谷歌应用程序,Hotmail的),但都做同样的事情。请帮忙!

var sendWithAttachment = function(userMail, subject, html, attachment_path, cb){ 
    var smtpTransport = nodemailer.createTransport("SMTP",{ 
     service: "Gmail", 
     auth: { 
      user: "[email protected]", 
      pass: "password" 
     } 
    }); 

    var mailOptions = { 
     from: "Labs <[email protected]>", 
     to: userMail, 
     subject: subject || "[blank]" 
     html: html || "[none]" 
     generateTextFromHTML: true, 
     attachments: [ 
      { // filename and content type is derived from path 
       path: attachment_path 
      }, 
      { // utf-8 string as an attachment 
       filename: 'check.txt', 
       content: 'checking that some attachments work...' 
      }, 
     ], 
    }; 

    smtpTransport.sendMail(mailOptions, function(error, response){ 
     if(error){ 
      console.log(error); 
      cb(error, null); 
     }else{ 
      console.log("Message sent: " + response.message); 
      cb(null, response); 
     } 
     smtpTransport.close(); 
    }); 
}; 
+0

使用单一对象中的附件,无论是'path'否则。作为'Path' - '文件或URL的路径(数据uris也是允许的),如果你想流式传输文件而不是包含它(更适合更大的附件)。所以删除这部分,并尝试.... – Ravi 2014-10-07 07:16:27

+0

文档说,你可以包括这样的多个附件。我只用一个附件尝试过,问题是一样的...... – 2014-10-07 18:56:03

+0

如果您已经取得了这方面的进展,请发布您的答案,因为我面临类似的问题。附件只是不发送,但我收到电子邮件。 – 2014-10-30 15:29:12

我通过重命名contentcontents解决了这个问题。我正在阅读最新版本的nodemailer的最新文档。您可以阅读文档的版本低于1.0 这里https://github.com/andris9/Nodemailer/blob/0.7/README.md

+0

类似地,1.0中的'path'参数是旧版本中的'filePath' – 2014-11-18 02:26:38

var mailOptions = { 
    ... 
    attachments: [ 
     { // utf-8 string as an attachment 
      filename: 'text1.txt', 
      content: 'hello world!' 
     }, 
     { // binary buffer as an attachment 
      filename: 'text2.txt', 
      content: new Buffer('hello world!','utf-8') 
     }, 
     { // file on disk as an attachment 
      filename: 'text3.txt', 
      path: '/path/to/file.txt' // stream this file 
     }, 
     { // filename and content type is derived from path 
      path: '/path/to/file.txt' 
     }, 
     { // stream as an attachment 
      filename: 'text4.txt', 
      content: fs.createReadStream('file.txt') 
     }, 
     { // define custom content type for the attachment 
      filename: 'text.bin', 
      content: 'hello world!', 
      contentType: 'text/plain' 
     }, 
     { // use URL as an attachment 
      filename: 'license.txt', 
      path: 'https://raw.github.com/andris9/Nodemailer/master/LICENSE' 
     }, 
     { // encoded string as an attachment 
      filename: 'text1.txt', 
      content: 'aGVsbG8gd29ybGQh', 
      encoding: 'base64' 
     }, 
     { // data uri as an attachment 
      path: 'data:text/plain;base64,aGVsbG8gd29ybGQ=' 
     } 
    ] 
} 

它在nodemailer的文档的问题。使用'filePath'更改'路径'以定义路径并将文本内容更改为内容。为我工作。

var mailOptions = { 
    ... 
    attachments: [ 
     { // utf-8 string as an attachment 
      filename: 'text1.txt', 
      contents: 'hello world!' 
     }, 
     { // utf-8 string as an attachment 
      filename: 'text1.txt', 
      filePath: 'text.txt' 
     }, 
    ] 
} 
+0

使用'filePath'更改'path'对我有用。谢谢:) – 2017-08-30 19:08:15

+0

欢迎您:) – 2017-09-07 06:02:25

//简单的代码由nodemailer发送电子邮件与附件

var smtpTransport = nodemailer.createTransport("SMTP",{ 
service: "Gmail", 
auth: { 
     user: "[email protected]", 
     pass: "passport" 
    } 
}); 

fs.readFile("path/logfile.txt", function (err, data) { 
    //creating simple built-in templating 
    var templateSender = { 
     from: 'MK <[email protected]>', // sender address 
     to: '[email protected]', // list of receivers 
     subject: "Attachment", // Subject line 
     body: 'mail content...', 
     attachments: [{ filepath: 'path/logfile.txt', filename: 'logfile.txt', contents: data}] 
    }; 

    // send mail with defined transport object 
    smtpTransport.sendMail(templateSender, function(error, success){ 
     if(error){ 
      return console.log(error); 
     }   
     smtpTransport.close(); 
    }); 

});