邮件宝石:在浏览器中呈现收到的电子邮件/完整解析html部分 [英] Mail Gem: Render received email in browser / Full parse of html parts

查看:162
本文介绍了邮件宝石:在浏览器中呈现收到的电子邮件/完整解析html部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与邮件宝石一起在Rails 4应用程序中接收电子邮件.每隔10分钟左右,通过Gem从邮箱通过POP抓取邮件.

我需要渲染这些电子邮件-大多数以HTML格式呈现-并无法以HTML格式保存正文,更不用说处理诸如嵌入式图像之类的东西了.我可能在这里遗漏了一些东西-在StackOverflow上看起来都很完整,但是还没有找到相关问题的答案.

我一直在与

message.body.decoded

,还查看了html_part v.text_part-但我看不到一种仅获取随附HTML的方法.看来我需要剥离标题,而Mail Gem似乎将其保留在正文中,然后还要处理任何内联附件.当然,必须存在一颗宝石..?还是一种方法...?您的建议很有价值.

我确实找到了 Mailcatcher Gem -但这确实是它自己的Sinatra应用.我可能只是尝试改编这种宝石,但这似乎需要大量工作.当然其他人已经解决了这个问题.

我还将对如何最好地在MySQL中存储消息正文提出建议-正在考虑使用大文本或Blob类型.

谢谢!

解决方案

前几天我一直在工作,我认为自己找到了解决方案.

首先,您必须确定邮件是否是多部分邮件,

mail.multipart?

如果邮件不是多部分邮件,则可以得到这样的正文

mail.body.decoded

如果邮件是多部分的,则必须选择是否要存储html或文本部分.但是,当然,您可以同时存储这两个部分.

#for html part

html_decoded_body = nil

mail.parts.each do |part|
  if part.content_type.include?('html')
    html_decoded_body = part.body.decoded
  end
end

 #for text part

 text_decoded_body = nil

 mail.parts.each do |part|
  if part.content_type.include?('text')
    text_decoded_body = part.body.decoded
  end
end

然后,您需要先强制编码,然后再将此主体保存到数据库中.

(html_decoded_body || text_decoded_body).force_encoding('UTF-8')

这时,您已经准备好将电子邮件正文保存在数据库中,但是稍后,当您尝试呈现此html时,您会发现仍然可以看到一些奇怪的内容,并且链接和图像也不会出现.工作.原因很简单.电子邮件使用了一些奇怪的编码(有关更多信息,请阅读此文章, https://en.wikipedia.org/wiki/Quoteed-printable ),您就可以再一次解码它们的主体了.我为此使用了这个JS库: https://github.com/mathiasbynens/quoted-printable.

最后一个代码在javascript中.

quotedPrintable.decode(email_body)

还有哇啦.

I am working with the Mail Gem to receive emails in my Rails 4 application. The mails are grabbed via POP from a mailbox, every 10 minutes or so, via the Gem.

I need to render these emails - mostly in HTML format - and am having trouble saving the body in HTML, not to mention working with things like embedded images. I'm probably missing something here- looked all over StackOverflow but haven't seen an answer to related questions yet.

I have been working with

message.body.decoded

and also looked at the html_part v. text_part - but I don't see a method to get to just the enclosed HTML. Looks like I need to strip the headers, which the Mail Gem seems to leave in the body- then also deal with any inline attachments. Surely a gem for this must exist..? Or an approach...? Your advice is valued.

I did find the Mailcatcher Gem - but that is really its own Sinatra app. I might just try adapting this gem but that appears to be a lot of work. Surely someone else has already dealt with this problem..?

I would also value a suggestion on how to best store the message body in MySQL - am thinking large text or blob type.

Thank you!

解决方案

I worked on that last days and I think that I found a solution.

In the first place you have to find out if mail is multipart,

mail.multipart?

If mail isn't multipart, you can get body like that,

mail.body.decoded

If mail is multipart, you have to choose if you want to store html or text part. But of course you can store both parts, it's on you.

#for html part

html_decoded_body = nil

mail.parts.each do |part|
  if part.content_type.include?('html')
    html_decoded_body = part.body.decoded
  end
end

 #for text part

 text_decoded_body = nil

 mail.parts.each do |part|
  if part.content_type.include?('text')
    text_decoded_body = part.body.decoded
  end
end

Then you need to force encoding before saving this body in your database.

(html_decoded_body || text_decoded_body).force_encoding('UTF-8')

In this point you're ready to save email body in your database but later, when you'll try to render this html, you'll find out that you can still see some weird stuff and your links and images won't work. The reason is very simple. Emails are using some weird coding (read this article for more information https://en.wikipedia.org/wiki/Quoted-printable) and you have decode their body one more time. I'm using for that this JS library: https://github.com/mathiasbynens/quoted-printable.

So last code is in javascript.

quotedPrintable.decode(email_body)

And wuaala.

这篇关于邮件宝石:在浏览器中呈现收到的电子邮件/完整解析html部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆