2017-05-24 64 views
1

我想附上zip文件。但它不起作用任何附件。nodemailer附件不起作用

這是我的源代碼。

var express = require('express'); 
var router = express.Router(); 
var nodemailer = require('nodemailer'); 
var fs = require('fs'); 
var mailinfo = require('../config/mail_info').info; 

var smtpTransport = nodemailer.createTransport({ 
    host: mailinfo.host, 
    port: mailinfo.port, 
    auth: mailinfo.auth, 
    tls: mailinfo.tls, 
    debug: true, 
}); 

router.post('/',function(req,res){ 
    var emailsendee = req.body.emailAddress; 
    console.log(emailsendee); 
    var emailsubject = "Requested File"; 
    var emailText = "test"; 
    var emailFrom = '[email protected]'; 

    var mailOptions={ 
     from : "test <[email protected]>", 
     to : emailsendee, 
     subject : emailsubject, 
     html : '<h1>' + emailText+ '</h1>'; 
     attachments : [ 
      { 
       filename : '',//i just put black make you understand esaily 
       path : ''//what i did is under this code 
      } 
     ] 
    }; 

    console.log(mailOptions); 
    smtpTransport.sendMail(mailOptions, function(error, response){ 
    if(error){ 
     console.log(error); 
     res.end(); 
    }else{ 
     console.log(response); 
     res.end(); 
    } 
}); 
}); 

module.exports = router; 

我試圖爲這些附加文件

enter code here 
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}] 

它仍然發送郵件,而附接。 當此代碼無法讀取文件時出現錯誤。 所以我想這是不工作,因爲閱讀文件。 和我讀了一些關於stackoverflow的問題,它和我有類似的錯誤。

我固定路徑 - >文件路徑 和固定streamSource - >路徑 我的nodemailer版本是4.0.1。 幫我發郵件給zip文件。

回答

0

我使用完全相同版本的nodemailer4.0.1此刻),並且我正在成功發送帶有附件的電子郵件。

你的第一個代碼片段看起來很有希望:)

但第二部分

我想這些對於附加文件

輸入代碼在這裏

附件:[{文件名:'test.log',streamSource:fs.createReadStream('./ test.log'}]

不看的權利在所有...

請參考nodemailer docs

文件名StreamSource的沒有的mailOptions對象

一個有效參數

DOCS示例

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/nodemailer/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=' 
     }, 
     { 
      // use pregenerated MIME node 
      raw: 'Content-Type: text/plain\r\n' + 
       'Content-Disposition: attachment;\r\n' + 
       '\r\n' + 
       'Hello world!' 
     } 
    ] 
} 

,你可以看到你應該改變文件名StreamSource的內容

// WRONG 
attachments:[{ fileName: 'test.log', streamSource: fs.createReadStream('./test.log'}] 

// RIGHT 
attachments:[{ filename: 'test.log', content: fs.createReadStream('./test.log'}] 

祝您好運!我希望這對你有所幫助:)