2

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.

Community
  • 1
  • 1
Anthony E
  • 11,072
  • 2
  • 24
  • 44

1 Answers1

0

In my application I use only

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

It works fine for me

devanand
  • 5,116
  • 2
  • 20
  • 19
  • Thanks, already tried that but as I explained in my question above the URL doesn't display correctly in the `image_tag`. – Anthony E Apr 08 '16 at 13:16