如何使用 Mailgun 和 Node.js 将图像作为附件发送?

How to Send Images as Attachments with Mailgun and Node.js?

我正在尝试将图像作为电子邮件附件发送,但我不知道如何完成此操作。

我正在使用 Mailgun 发送邮件,Cloudinary 上传图片,MongoDB 作为我的数据库,Node.js/Express 作为我的后端。

用户进程是这样​​的:

显然这并不理想,因为您需要单独单击每个 link 才能查看和下载图像。我想将它们直接附加到电子邮件中,以便用户可以更轻松地下载图像。

我查看了 Mailgun 的文档,但似乎无法将非本地图像作为附件发送。有什么我想念的吗?

我已经尝试使用 Mailgun 的 'inline' 和 'attachment' 参数,但我最终收到一条错误消息,指出无法找到 file/directory。

var pictures = [];
        post.images.forEach(function(photos){
            pictures.push(photos + " ");
            return pictures;
        });

var attch = new mailgun.Attachment({data: pictures[0], filename: "picture"});
        var data = {
            from: "email <email@email.com>",
            to: "email@email.com",
            subject: 'this is an email',
            html: 'here is a new post and here are the images in that post',
            attachment: attch
        };

预期结果是一封附有新 post 图片的电子邮件,或者在本例中是 post 的单张图片。

实际结果是这个错误信息:

events.js:183
  throw er; // Unhandled 'error' event
  ^

Error: ENOENT: no such file or directory, stat 'https://res.cloudinary.com/user/image/upload/image.jpg '

mailgun.js 包将接受附件作为文件路径、缓冲区和流。要从外部 URL 附加图像,请使用流,

var request = require('request');
var image = request(pictures[0]);
var data = {
    from: "email <email@email.com>",
    to: "email@email.com",
    subject: 'this is an email',
    html: 'here is a new post and here are the images in that post',
    attachment: image
};

这是来自 mailgun.js

的示例代码
var request = require('request');
var file = request("https://www.google.ca/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");

var data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'serobnic@mail.ru',
  subject: 'Hello',
  text: 'Testing some Mailgun awesomeness!',
  attachment: file
};

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

参考:https://www.npmjs.com/package/mailgun-js#attachments