使用ejs + emailjs时Outlook中未显示base64图像 [英] base64 images not displaying in Outlook when using ejs + emailjs

查看:1007
本文介绍了使用ejs + emailjs时Outlook中未显示base64图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我混合使用了 ejs

效果很好,因为我能够将生成的字符串直接复制并粘贴到浏览器中,并查看图像.但是,当我通过emailjs发送电子邮件时,Outlook将+转换为+.当我在Gmail中查看原始邮件"时,base64被拆分为块".每个块都在换行符上,并且每行以=结尾.如果我使用Gmail的版本并删除=和换行符,然后将其粘贴到浏览器中,则整个图片会完美加载,但是无论用户是否在我的联系人列表中,它都拒绝加载其他地方.

这是我用来发送电子邮件的代码:

// Host, username, password and SSL (false) all set above here
server.send({
    text: myTemplate,
    from: "me@example.com",
    to: "someone@example.com",
    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>

有任何提示吗?

我尝试在"attachment"属性中设置标题,但是没有运气

解决方案

Outlook使用Word渲染图像,而Word不支持嵌入式(src="data:image")图像.

您需要将图像附加为文件,并将Content-ID MIME标头设置为与HTML正文中图像(<img src="cid:xyz">)上的cid属性匹配的值.

I'm using a mix of ejs and emailjs to send out emails in my node.js app when various events happen. I'd like to embed a base64 image (a small logo) into the email when it sends, but both Outlook and Gmail (plus Inbox by Google) fail to render the image.

I'm using this bit of code to find the mime type of the image and put together the base64 string:

MyApp.prototype.ImageToBase64 = function(image) {
  var mime = require("mime")
  file = fs.readFileSync(__dirname + "/images/" + image, { encoding: 'base64'})
  return 'data:' + mime.lookup(image) + ';base64,' + file;
}

That works great, because I'm able to copy and paste that resulting string right into my browser and see the image. But when I send the email via emailjs, Outlook converts the +s to &#43;. When I "View Original" in Gmail, the base64 is split up into 'chunks'. Each chunk is on a newline and each line ends with a =. If I take Gmail's version and remove the = and newline then paste it into my browser, the whole picture loads perfectly, but it just refuses to load anywhere else, regardless of whether the user is in my contact list or not.

Here's the code I'm using to send the email:

// Host, username, password and SSL (false) all set above here
server.send({
    text: myTemplate,
    from: "me@example.com",
    to: "someone@example.com",
    subject: "Testing",
    attachment: [
     {data:myTemplate, alternative:true}
    ]
  })

And the template looks like this (truncated, as the other bits aren't important):

  <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>

Any hints?

EDIT: I tried setting the headers in the "attachment" property, but with no luck

解决方案

Outlook uses Word to render the images, and Word does not support embedded (src="data:image") images.

You need to attach the image as a file and set the Content-ID MIME header to the value matching the cid attribute on the image (<img src="cid:xyz">) in the HTML body.

这篇关于使用ejs + emailjs时Outlook中未显示base64图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆