当各种事件发生时,我使用ejs和emailjs的混合发送电子邮件到我的node.js应用程序中。我想在发送邮件时在邮件中嵌入base64图像(小徽标),但Outlook和Gmail(以及Google收件箱)无法呈现图像。当使用ejs + emailjs时,在Outlook中不显示base64图像
我使用这段代码,找到图像的MIME类型和放在一起的base64字符串:
MyApp.prototype.ImageToBase64 = function(image) {
var mime = require("mime")
file = fs.readFileSync(__dirname + "/images/" + image, { encoding: 'base64'})
return 'data:' + mime.lookup(image) + ';base64,' + file;
}
这一工程的伟大,因为我能够复制并粘贴生成的字符串直接进入我的浏览器并看到图像。但是,当我通过emailjs发送电子邮件时,Outlook会将+
s转换为+
。当我在Gmail中查看原始内容时,base64被分成'块'。每个块都在换行符上,每行以=
结尾。如果我使用Gmail的版本并删除=
和换行符,然后将其粘贴到我的浏览器中,则整个图片加载完美,但它拒绝在其他任何地方加载,无论用户是否在我的联系人列表中。
这里是我用来发送电子邮件的代码:
// Host, username, password and SSL (false) all set above here
server.send({
text: myTemplate,
from: "[email protected]",
to: "[email protected]",
subject: "Testing",
attachment: [
{data:myTemplate, alternative:true}
]
})
而且模板看起来像这样(截断,因为其他位并不重要):
<body>
<p><img src="<%- ImageToBase64("logo.png") %>"></p>
<h1><%= Name %> has been triggered</h1>
<p><%= Name %> has been triggered. It was triggered on <%= TheDate %></p>
任何提示?
编辑:我试图在“连接”属性设置标头,但没有运气