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.