Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails attachments inline are not shown correctly in gmail

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?

like image 212
roman Avatar asked Dec 02 '11 13:12

roman


3 Answers

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.

like image 144
roman Avatar answered Nov 08 '22 18:11

roman


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.

like image 22
jmarceli Avatar answered Nov 08 '22 17:11

jmarceli


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.

like image 1
Gemtastic Avatar answered Nov 08 '22 17:11

Gemtastic