Rails attachments inline are not shown correctly in gmail

我只是一个虾纸丫 提交于 2019-11-26 17:18:44

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8356924/rails-attachments-inline-are-not-shown-correctly-in-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!