2016-08-26 12 views
0

nodemailer 0.7.1を使用して添付ファイルを送信しようとしています。添付ファイルは正常に送信されますが、開こうとするとERROR OPENING FILEが表示されます。nodemailerでNodemailer添付ファイルが動作していない0.7.1

は、ここに私のコードです:

var nodemailer = require("nodemailer"); 

var transport = nodemailer.createTransport("SMTP", { 
    host: "smtp.gmail.com", // hostname 
    secureConnection: true, // use SSL 
    port: <port>, // port for secure SMTP 
    auth: { 
     user: "[email protected]", 
     pass: "password" 
    } 
}); 

console.log("SMTP Configured"); 

var mailOptions = { 
    from: '[email protected]', // sender address 
    to: '[email protected]', // list of receivers 
    subject: 'Report for Test Result', // Subject line 
    text: 'Contains the test result for the test run in html file', // plaintext body 
    attachments: [ 
     { 
      'filename': 'results.txt', 
      'filePath': './result/results.txt', 
     } 

    ] 
}; 
transport.sendMail(mailOptions, function (error, response) { 
    if (error) { 
     console.log(error); 
    } else { 
     console.log("Message sent: " + response.message); 
    } 

}); 

大きな助けになるだろう、これを解決する方法上の任意の提案。

答えて

0

filePathの行をpath: './result/results.txt'に置き換えて試してみてください。

0

Google Cloud Consoleでアプリを作成し、ライブラリからGmail APIを有効にする必要があります。アプリの認証情報を取得します。認証情報をクリックし、許可されたリダイレクトURIskeepの代わりにこのリンクhttps://developers.google.com/oauthplaygroundと別のタブに次にこのリンクを開きますhttps://developers.google.com/oauthplayground/これの後に、あなたはあなたのclientIdとclientSecret.Andをsametimeに渡す必要がありますので、右側の設定記号をクリックしてチェックボックスをオンにします(つまり、独自のOAuth資格情報を使用します)。左側にはInput Your Own Scopesのようなプレースホルダーのテキストボックスがあり、このリンクはhttps://mail.google.com/のままで、APIの承認をクリックしてトークンのExchange認証コードをクリックすると、refreshTokenとaccessTokenがコード内に保持されます。あなたに役立ちます..

const nodemailer=require('nodemailer'); 
const xoauth2=require('xoauth2'); 
var fs=require('fs'); 
var transporter=nodemailer.createTransport({ 
service:'gmail', 
auth:{ 
    type: 'OAuth2', 
    user:'Sender Mail', 
clientId:'Your_clientId',//get from Google Cloud Console 
clientSecret:'Your clientSecret',//get from Google Cloud Console 
refreshToken:'Your refreshToken',//get from https://developers.google.com/oauthplayground 
accessToken:'Tor accessToken'//get from https://developers.google.com/oauthplayground 
}, 
}); 
fs.readFile("filePath",function(err,data){ 
var mailOptions={ 
from:' <Sender mail>', 
to:'receiver mail', 
subject:'Sample mail', 
text:'Hello!!!!!!!!!!!!!', 
attachments:[ 
{ 
    'filename':'filename.extension',//metion the filename with extension 
    'content': data, 
    'contentType':'application/type'//type indicates file type like pdf,jpg,... 
}] 
} 
transporter.sendMail(mailOptions,function(err,res){ 
if(err){ 
    console.log('Error'); 
} 
else{ 
console.log('Email Sent'); 
} 
}) 
}); 
関連する問題