2016-05-14 15 views
3

ご回答いただきありがとうございます。私はnodemailer 0.7.1を使用するいくつかのコードを書いています。それは電子メールを送信し、電子メールにpdfを添付します。ただし、.pdf添付ファイルは、エンコードや切り捨てなどの際に壊れてしまいます。私がこれを言うのは、添付ファイル(すなわち、私がローカルに持っているもの)が512kbで、電子メールの添付ファイルが1kbだけであるという理由です。PDF添付ファイルNodeMailer

これはnodemailerを使用するコードです:私はブラウザとしてGoogleのクロムを使用していますが、無駄に他のブラウザで試してみました

var nodemailer = require("nodemailer"); 
 
var util = require("./util"); 
 
var env = require('./environment'); 
 

 
var smtpTransport = nodemailer.createTransport("SMTP",{ 
 
    service: env.service, 
 
    auth: { 
 
     user: env.user, 
 
     pass: env.password 
 
    } 
 
}); 
 

 
exports.sendAttachment = function(info, callback, debug) { 
 
    util.validatInput(info, ["body"] , function(err, info){ 
 
     if(err){ 
 
      util.errPrint(err, "serverUtil/nodemailer.sendAttachment", 1, function(message){callback(err);}); 
 
     }else { 
 
      var mailOptions={ 
 
       from : "[email protected]", 
 
       to : "[email protected]", 
 
       subject : "Application from " + info.userEmail, 
 
       text : info.body, 
 
       attachments: [ 
 
        { 
 
         fileName: 'file.pdf',    //This needs to be the link to the form, or the actual form 
 
         filePath: 'file.pdf', 
 
         contentType: "application/pdf" 
 
        } 
 
       ] 
 
      } 
 

 
      smtpTransport.sendMail(mailOptions, function(error, response){ 
 
       
 
       if(error){ 
 
        console.log(error); 
 
        callback(err); 
 
       } 
 
       else{ 
 
        console.log("Message sent: " + response.message); 
 
        callback({msg: "form sent"}); 
 
       } 
 
      }); 
 
     } 
 
    }) 
 
}

。明らかに、ブラウザ自体は、pdf自体のデータがここでの問題であるため、これと何ら関係がありません。

私は問題を避けるためにファイルを同じディレクトリに置き、現在のディレクトリのファイルよりも前に './'しました。私も 'filepath'を 'path'に変更してから、添付ファイルを一切送信しませんでした。

問題は「添付ファイル」の配列にあると思います。おそらく、フィールドが正しくないか、いくつかの情報を追加する必要があります。

私がやっていることではなくストリームにする必要があるかどうか誰にでも分かります。

答えて

1
var api_key = 'key-6b6987887a1aa9489958a5f280645f8b'; 
var domain = 'sandboxcd1a6d15d41541f38519af3f5ee93190.mailgun.org'; 
var mailgun = require('mailgun-js')({apiKey: api_key,domain:domain}); 
var path = require("path"); 

var filepath = path.join(__dirname, 'wacc.pdf'); 

var data = { 
    from: '[email protected]', 
    to: '[email protected]', 
    subject: 'Today Test', 
    text: 'Sending Test', 
    attachment: filepath 
}; 

mailgun.messages().send(data, function (error, body) { 
    console.log(body); 
}); 
1

私は、filePath属性とcontentType属性を取り除き、代わりにstreamSourceを配置する必要があることが判明しました。私もfs.createReadStreamを使う必要がありました。興味があればここにコードがあります。

var nodemailer = require("nodemailer"); 
 
var util = require("./util"); 
 
var env = require('./environment'); 
 
var fs = require('fs'); 
 
var path = require('path'); 
 
var smtpTransport = nodemailer.createTransport("SMTP", { 
 
    service: env.service, 
 
    auth: { 
 
    user: env.user, 
 
    pass: env.password 
 
    } 
 
}); 
 

 
exports.sendAttachment = function(info, callback, debug) { 
 
    util.validatInput(info, ["body"], function(err, info) { 
 
    if (err) { 
 
     util.errPrint(err, "serverUtil/nodemailer.sendAttachment", 1, function(message) { 
 
     callback(err); 
 
     }); 
 
    } else { 
 
     var filePath = path.join(__dirname, 'file.pdf'); 
 

 
     var mailOptions = { 
 
     from: "[email protected]", 
 
     to: "[email protected]", 
 
     subject: "Application from " + info.userEmail, 
 
     text: info.body, 
 
     attachments: [{ 
 
      fileName: 'file.pdf', //This needs to be the link to the form, or the actual form 
 
      // filePath: './file.pdf', 
 
      streamSource: fs.createReadStream(filePath) 
 
      // , contentType: "application/pdf" 
 
     }] 
 
     } 
 

 
     smtpTransport.sendMail(mailOptions, function(error, response) { 
 

 
     if (error) { 
 
      console.log(error); 
 
      callback(err); 
 
     } else { 
 
      console.log("Message sent: " + response.message); 
 
      callback({ 
 
      msg: "form sent" 
 
      }); 
 
     } 
 
     }); 
 
    } 
 
    }) 
 
}

関連する問題