[英]Sending embedded image in email using sendgrid in node.js
I want to send an email with embedded image from node js using sendgrid.我想使用 sendgrid 从节点 js 发送带有嵌入图像的电子邮件。 I use the following code
我使用以下代码
var base64Img = require('base64-img');
const sgMail = require('@sendgrid/mail');
var base64str = base64_encode("logo.png");
function base64_encode(file) {
var bitmap = fs.readFileSync(file);
return new Buffer(bitmap).toString("base64");
}
sendMail(["example1@gmail.com"],base64str);
function sendMail(emails,data)
{
sendgrid.send({
to: emails[0],
from: 'example2@gmail.com',
subject: 'Test Mail',
attachments: [
{
filename: "logo.png",
type : "image/png",
content: data,
content_id: "myimagecid",
disposition : "inline"
}
],
html:"Please look on the image <img src='content_id:myimagecid' alt='no image found'/>",
}, function (err) {
if (err) {
response.json({ message: 'Selected but Mail not sended and Error is'+err });
console.log("Mail error",err);
} else {
console.log("Success Mail sended ");
response.json({ message: 'Selected and Mail sended' });
}
});
}
Here I used disposition : "inline" but still the image send as an attachment.在这里,我使用了disposition : "inline"但图像仍然作为附件发送。 Can anyone help me please?
有人可以帮我吗?
You are defining your file as an attachment, therefore it ends up being that.您将文件定义为附件,因此最终就是这样。 Instead, you need to define it as a file, and then call it in your html code.
相反,您需要将其定义为一个文件,然后在您的 html 代码中调用它。
If you read through this article...如果你通读这篇文章...
https://sendgrid.com/blog/embedding-images-emails-facts/ , https://sendgrid.com/blog/embedding-images-emails-facts/ ,
... you'll see this is actually not a recommended approach. ...您会发现这实际上不是推荐的方法。 Rather you may want to embed your base64'd image straight into your html.
相反,您可能希望将 base64 图像直接嵌入到您的 html 中。
如果你没有很多图像,你可以使用Inline Embedding
<img alt="My Image" src="data:image/jpeg;base64,/9j/4S/+RXhpZgAATU0AKgAAAAgACAESAAMAENkDZ5u8/61a+X...more encoding" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.