inline images with ruby mail gem

帅比萌擦擦* 提交于 2019-12-10 09:28:28

问题


Sorry if I'm missing a good article, but I can't find a good example on how to use inline images using ruby with the mail gem (I'm not using RoR) The best example I could find is here, but I don't understand where the .cid method comes from.

here's an excerpt from the above mentioned post where the .cid method is used.

html_part do
    content_type 'text/html; charset=UTF-8'
    body "<img width=597 height=162 id='Banner0' src='cid:#{pic.cid}'>"
end

I have the mail gem working and it can send images as attachments but I need them to be displayed inside the email, without having the need to open attachments.


回答1:


The Mail gem ist quite well maintained, so it really helps to look into the documentation. Your provided snipped misses an integral part: the definition of pic:

pic = attachments['banner.png']

With this information, you can easily find the documentation on attachments, which will yield this bit:

You can also search for specific attachments:

# By Filename
mail.attachments['filename.jpg']   #=> Mail::Part object or nil

In the documentation to Mail::Part, you will then find the definition of the cid method:

def cid
  add_content_id unless has_content_id?
  uri_escape(unbracket(content_id))
end

(sadly, this method is not documented, but it's easy to infer that "cid" stands for "content id").

Finally, RFC2557 defines the usage of CID URIs to identify and reference encapsulated documents (such as images) within emails. This is where

body "<img ... src='cid:...'>"

comes from.



来源:https://stackoverflow.com/questions/24273954/inline-images-with-ruby-mail-gem

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