RomanKapitonov RomanKapitonov - 2 years ago 100
Ruby Question

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'] ="#{Rails.root.to_s + '/app/assets/images/blank_500x500.png'}")
attachments.inline['discount-deal-triangle'] ="#{Rails.root.to_s + '/app/assets/images/discount-deal-triangle.png'}")
mail(:to =>, :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?

Answer Source

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 =>"#{Rails.root.to_s + '/app/assets/images/blank_500x500.png'}"),
                                :mime_type => "image/png",
                                :encoding => "base64"
attachments.inline['discount-deal-triangle'] = {
                                :data =>"#{Rails.root.to_s + '/app/assets/images/discount-deal-triangle.png'}"),
                                :mime_type => "image/png",
                                :encoding => "base64"

That did the trick for me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download