Unable to render inline attachments in ActionMailer

我与影子孤独终老i 提交于 2019-12-10 22:14:54

问题


I'm trying to render inline images in a standard ActionMailer view using the approach outlined here Rails attachments inline are not shown correctly in gmail.

My code in my mailer:

attachments.inline["sample.png"] = {
  mime_type: "image/png",
  encoding: "base64",   # Including this line causes byte sequence error
  data: File.read('public/sample.png')
}

In mail view:

image_tag(attachments["sample.png"].url)

Gives the familiar ruby UTF-8 byte sequence error:

invalid byte sequence in UTF-8

To get around this I tried the following:

attachments.inline["logo.png"] = {
      mime_type: "image/png",
      data: Base64.encode64(File.read('public/logo.png')) 
    }

and also

attachments.inline["logo.png"] = File.read('public/logo.png')

Using the same image_tag syntax shown above.

Both of these resolve the UTF error, but I'm left with this nonsensical URL in the view:

<img src="cid:5707a64ededbc_7bd83ffd648601e029875@localhostname.mail">

The PNG image is valid and renders properly in a standard HTML view. I'm using Rails 4.2.5 with Ruby 2.2.4

EDIT

This works:

Mailer:

attachments.inline["cape.png"] = {
  mime_type: "image/png",
  # encoding: "base64",
  content: Base64.encode64(File.read(Rails.root.join("public/", "cape.png")))
}

View:

= image_tag "data:image/png;base64,#{attachments['logo.png'].read}"

Very awkward, however, and I'm still wondering why the conventional approach doesn't work.


回答1:


In my application I use only

attachments.inline["logo.png"] = File.read('public/logo.png')

It works fine for me



来源:https://stackoverflow.com/questions/36499884/unable-to-render-inline-attachments-in-actionmailer

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