如何使用nodemail将URL图像附加到电子邮件?

时间:2015-09-07 10:29:17

标签: node.js request nodemailer

我正在尝试通过网址链接将图片附加到我发送的电子邮件中。这是我的代码,

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: '__@yahoo.co.uk',
            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: '__@yahoo.co.uk',
            to: '__@yahoo.co.uk',
            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');
}

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

1 个答案:

答案 0 :(得分:0)

Nodemailer 自动从提供的 URL 获取和附加文件。这是detailed guide of different ways to attach files

attachments: [{
        filename: "iIAS1wE.jpg",
        path: "http://i.imgur.com/iIAS1wE.jpg",
    }]