2017-05-24 29 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でいくつかの質問を読んでください。

i固定パス - >ファイルパス 固定ストリームソース - >パス 私のnodemailerバージョンは4.0.1です。 私はzipファイルでメールを送信するのに役立ちます。

答えて

0

私はnodemailer(現時点では4.0.1)と全く同じバージョンを使用しており、添付ファイル付きのメールを送信しています。

あなたの最初のコードスニペットは有望に見える:)

しかし、第二部

私は

がここ

添付ファイルのコードを入力して、ファイルを添付するためにこれらを試してみました:[{ファイル名を: 'test.log'、streamSource:fs.createReadStream( './ test.log'}]

は...右のすべての

を見ていないnodemailer docs

fileNameにのStreamSourceを参照してくださいmailOptionsオブジェクト

の有効なパラメータではありません

文書の例

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'}] 

幸運にファイル名を変更する必要があります見ることができるように!これがあなたに役立つことを願っています:)

関連する問題