2014-10-07 92 views
2

我使用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

回答

2

我通過重命名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

0
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=' 
     } 
    ] 
} 
4

它在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

0

//簡單的代碼由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(); 
    }); 

});