inline images with ruby mail gem

天大地大妈咪最大 提交于 2019-12-05 12:36:50

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.

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