我想发送带有一些图像的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)
}
如果您需要更多信息来解决此问题。请告诉我。谢谢。
您需要将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'
}
]
};