Rails attachments inline are not shown correctly in gmail

天涯浪子 提交于 2019-11-27 15:40:40

After all i found a solution: all you need to do is set the mime-type and encoding of the attachment.

attachments.inline['blank'] = {
                                :data => File.read("#{Rails.root.to_s + '/app/assets/images/blank_500x500.png'}"),
                                :mime_type => "image/png",
                                :encoding => "base64"
                              }
attachments.inline['discount-deal-triangle'] = {
                                :data => File.read("#{Rails.root.to_s + '/app/assets/images/discount-deal-triangle.png'}"),
                                :mime_type => "image/png",
                                :encoding => "base64"
                              }

That did the trick for me.

jmarceli

Use file extension in inline array. Example:

attachments.inline['blank.png'] = 
  File.read(Rails.root.join('app', 'assets', 'images', 'blank_500x500.png')

This way Rails will guess the file mime_type and encoding. At least Rails 4.2 will do so.

You may also refer to https://stackoverflow.com/a/25810153/2041318 where you can find nice helper method for mailer inline images.

Worth mentioning since my question I had when I found this question was the same, but a different root cause.

If you are running Rails 4 and have an issue with showing an image in Gmail (But not in Outlook365 or the OSX mailer client) make sure you're not trying to show a .svg file. Gmail does not support them as of this date I'm writing this and you'll need a .jpg or .png fallback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!