如何使用SendGrid将图像嵌入到Node.js中的html邮件中

问题描述 投票:0回答:1

我想发送带有一些图像的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个回答
0
投票

您需要将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'
    }
  ]
};
© www.soinside.com 2019 - 2024. All rights reserved.