【问题标题】:How to embed image to html mail in Node.js with SendGrid如何使用 SendGrid 将图像嵌入到 Node.js 中的 html 邮件
【发布时间】:2019-03-10 17:41:35
【问题描述】:

我想发送带有一些图片的 HTML 邮件。
我正在使用名为“sendgrid-nodejs”的库。
但是,我无法做到这一点并找到与之相关的任何文档。

我的代码。

const fs = require('fs');

function base64_encode(file) {
  var bitmap = fs.readFileSync(file);
  return new Buffer(bitmap).toString('base64');
};


const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'to address',
  from: 'from address',
  subject: 'subject',
  html: '<strong>Some Text</strong><img src="cid:12345" alt="test image" />',
  files: [
    {
      filename: 'test image',
      contentType: 'image/jpeg',
      cid: '12345',
      content: base64_encode('test.jpg')
    }
  ]
};

try {
  sgMail.send(msg)
} catch(err) {
  console.log(err)
}

如果您需要更多信息来解决此问题。请告诉我。谢谢。

【问题讨论】:

    标签: node.js sendgrid-api-v3


    【解决方案1】:

    您需要在附件中添加disposition: inline

    const msg = {
      to: 'to address',
      from: 'from address',
      subject: 'subject',
      html: '<strong>Some Text</strong><img src="cid:12345" alt="test image" />',
      files: [
        {
          filename: 'test image',
          contentType: 'image/jpeg',
          cid: '12345',
          content: base64_encode('test.jpg'),
          disposition: 'inline'
        }
      ]
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2011-07-12
      • 2012-03-30
      • 2017-07-04
      • 2011-09-07
      • 2022-10-13
      相关资源
      最近更新 更多