在 node.js 中使用 sendgrid 在电子邮件中发送嵌入图像

编程入门 行业动态 更新时间:2024-10-04 01:27:24

在 node.js 中使用 sendgrid 在电子邮件中发送嵌入<a href=https://www.elefans.com/category/jswz/34/1771430.html style=图像"/>

在 node.js 中使用 sendgrid 在电子邮件中发送嵌入图像

我想使用 sendgrid 从节点 js 发送带有嵌入图像的电子邮件。我使用以下代码

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(["[email protected]"],base64str);
function sendMail(emails,data)
{
sendgrid.send({
      to: emails[0],
      from: '[email protected]',
      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' });
    }
  });

}

这里我使用了disposition : "inline" 但图像仍然作为附件发送。谁能帮帮我吗?

回答如下:

您将文件定义为附件,因此它最终就是这样。 相反,您需要将其定义为一个文件,然后在您的 html 代码中调用它。

如果你通读这篇文章...

https://sendgrid/blog/embedding-images-emails-facts/ ,

...你会发现这实际上不是推荐的方法。相反,您可能希望将 base64 图像直接嵌入到您的 html 中。

更多推荐

在 node.js 中使用 sendgrid 在电子邮件中发送嵌入图像

本文发布于:2024-05-30 13:10:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   电子邮件   node   js   sendgrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!