[英]How to Send Images as Attachments with Mailgun and Node.js?
I am attempting to send images as attachments to emails but I am having trouble figuring out how to accomplish this. 我正在尝试将图像作为附件发送到电子邮件中,但是在弄清楚如何完成此操作时遇到了麻烦。
I am using Mailgun to send the mail, Cloudinary to upload the images, MongoDB as my database, and Node.js/Express as my backend. 我使用Mailgun发送邮件,使用Cloudinary上传图像,使用MongoDB作为我的数据库,使用Node.js / Express作为我的后端。
The user process goes like this: 用户进程如下所示:
Obviously this is not ideal because you need to click on each link individually to see and download the images. 显然,这并不理想,因为您需要单独单击每个链接以查看和下载图像。 I would like to attach them directly to the email so the user has an easier time downloading the images. 我想将它们直接附加到电子邮件,以便用户可以更轻松地下载图像。
I have looked at the documentation for Mailgun but it doesn't seem like non-local images can be sent as attachments. 我看过Mailgun的文档,但似乎不能将非本地图像作为附件发送。 Is there something I'm missing? 有什么我想念的吗?
I have tried using the 'inline' and 'attachment' parameters for Mailgun but I end up with an error message stating the file/directory cannot be located. 我曾尝试对Mailgun使用'inline'和'attachment'参数,但最终出现一条错误消息,指出无法找到文件/目录。
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
};
The expected result is an email with the attached images of the new post, or in this case a single image from that post. 预期结果是一封包含新帖子图像的电子邮件,在这种情况下,该电子邮件为该帖子的单个图像。
The actual result is this error message: 实际结果是此错误消息:
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 package will accept attachment as file path, buffer and stream. mailgun.js软件包将接受附件作为文件路径,缓冲区和流。 To attach your image from external URL use stream, 要通过外部网址附加图片,请使用流,
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
};
Here is the sample code from mailgun.js 这是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);
});
Reference : https://www.npmjs.com/package/mailgun-js#attachments 参考: https : //www.npmjs.com/package/mailgun-js#attachments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.