2015-09-07 114 views
0

我正在尝试通过URL附加一个图像链接到我发送的电子邮件。这是我的代码,如何使用nodemail将URL图像附加到电子邮件?

var nodemailer = require("nodemailer"); 
var fs = require("fs"); 
var request = require("request"); 

exports.index = function index(req, res){ 
    var transporter = nodemailer.createTransport("SMTP", { 
     service: 'yahoo', 
     auth: { 
      user: '[email protected]', 
      pass: '_' 
     } 
    }); 
    var attachedfile; 

     request.get('http://i.imgur.com/iIAS1wE.jpg', function (error, response, body) { 
      if (!error && response.statusCode == 200) { 
       attachedfile = body; 
      } 
     }); 
     var options = { 
      from: '[email protected]', 
      to: '[email protected]', 
      subject: 'hello', 
      text: 'hello! See attached files', 
      attachments: [ 
      {filename: 'starwars.jpg', contents: attachedfile }] 
     } 
     transporter.sendMail(options , function(err, response){ 
     if(err){ 
      console.log(err); 
     } else { 
      console.log('Message sent: ' + response.message); 
     } 
     transporter.close(); 
     }); 
    res.render('email/index'); 
} 

但是,当我检查电子邮件时没有图像...我在这里做错了什么? 感谢

回答

0

你不需要(你不发送大量电子邮件的情况下), nodemailer可以为你做手工获取文件:

attachments: [ 
      {path: 'http://i.imgur.com/iIAS1wE.jpg'}] 
+0

嗯,我已经试过,但图像文件没有附加到发送的电子邮件。 – creampiedonut

+0

我终于弄明白了,是'filePath',而不是'文件'。 – creampiedonut

相关问题