将从 gmail 中提取的图像嵌入自定义客户端 - rails
Embed extracted images from gmail to custom client - rails
我正在使用此 gem 与 Gmail 交互 API:https://github.com/gmailgem/gmail
要获取电子邮件,我有:
require 'gmail'
gmail = Gmail.connect("testemail@gmail.com", "password")
message = gmail.inbox.emails.last
puts message.message.html_part.body.raw_source
这个returns电子邮件的HTML格式,这是我需要的,但是图像在渲染时显示为丢失,并且URL很奇怪格式:
<div dir="ltr">see attached<div><br></div><div><img src="cid:ii_151afcf74cab7d3b" alt="Inline image 1" width="517" height="291"><br></div></div>
如何获取完整的 URL 图像,以便我可以在返回的 HTML 中渲染它们?
我知道 3 种可能的方法:https://www.codementor.io/tips/8112473532/strategies-for-serving-base64-encoded-images-in-html
我正在尝试应用第一个解决方案,我该怎么做?
附件硬编码在电子邮件正文中,如下所示:
This is the body of the message.
--frontier
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--frontier--
您可以解码附件,将其存储为临时文件并使用。
mail.attachments.each do | attachment |
# Attachments is an AttachmentsList object containing a
# number of Part objects
if (attachment.content_type.start_with?('image/'))
# extracting images for example...
filename = attachment.filename
begin
File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
rescue => e
puts "Unable to save data for #{filename} because #{e.message}"
end
end
end
我正在使用此 gem 与 Gmail 交互 API:https://github.com/gmailgem/gmail
要获取电子邮件,我有:
require 'gmail'
gmail = Gmail.connect("testemail@gmail.com", "password")
message = gmail.inbox.emails.last
puts message.message.html_part.body.raw_source
这个returns电子邮件的HTML格式,这是我需要的,但是图像在渲染时显示为丢失,并且URL很奇怪格式:
<div dir="ltr">see attached<div><br></div><div><img src="cid:ii_151afcf74cab7d3b" alt="Inline image 1" width="517" height="291"><br></div></div>
如何获取完整的 URL 图像,以便我可以在返回的 HTML 中渲染它们?
我知道 3 种可能的方法:https://www.codementor.io/tips/8112473532/strategies-for-serving-base64-encoded-images-in-html
我正在尝试应用第一个解决方案,我该怎么做?
附件硬编码在电子邮件正文中,如下所示:
This is the body of the message.
--frontier
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--frontier--
您可以解码附件,将其存储为临时文件并使用。
mail.attachments.each do | attachment |
# Attachments is an AttachmentsList object containing a
# number of Part objects
if (attachment.content_type.start_with?('image/'))
# extracting images for example...
filename = attachment.filename
begin
File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
rescue => e
puts "Unable to save data for #{filename} because #{e.message}"
end
end
end