6

Can anyone point me to the problem?

I'm using inline attachments in my rails 3.1 application mailer. The letter also contains images which are stored at amazon w3 servers.

The problem is that gmail doesn't display the letter correctly. I have inline attachments in the letter. But Gmail shows these files as attached ones. The letter also contains an attached html page which contains the letter itself. All gmail displays is a set of symbols which i suppose to a base64 version of one of the attached images.

See the screenshot.

I can't post the image due to the lack of necessary amount of rating so i posted it here.

Here is the code in my mailer:

attachments.inline['blank'] = File.read("#{Rails.root.to_s + '/app/assets/images/blank_500x500.png'}")
attachments.inline['discount-deal-triangle'] = File.read("#{Rails.root.to_s + '/app/assets/images/discount-deal-triangle.png'}")
mail(:to => @subscriber.email, :subject => subject)

And here is the code in the view file:

-if @image_url
  = image_tag( attachments['offer_image'].url, :id => 'offer_image', :width => "320", :height => "320")
-elsif @offer.image.nil?
  = image_tag( attachments['blank'].url, :id => 'offer_image', :width => "320", :height => "320")

I have omitted the details to make it simpler.

What am i doing wrong?

roman
  • 5,100
  • 14
  • 44
  • 77

3 Answers3

14

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.

roman
  • 5,100
  • 14
  • 44
  • 77
5

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.

Community
  • 1
  • 1
jmarceli
  • 19,102
  • 6
  • 69
  • 67
1

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.

Gemtastic
  • 6,253
  • 6
  • 34
  • 53
  • I am having THIS issue but am using .jpg images, so they are Outlook but not Gmail or Office 365. Would you know why? – Ben Smith Sep 05 '16 at 09:56
  • .jpg should be supported by all mail-clients. Have you checked to see that your mail client isn't hiding your content due to trust issues? SMTP can trigger restrictions in content screening. – Gemtastic Sep 06 '16 at 13:07