【问题标题】:Mail gem determine whether plaintext or html邮件 gem 判断是纯文本还是 html
【发布时间】:2012-04-23 01:15:11
【问题描述】:
我正在使用来自https://github.com/mikel/mail 的mail gem
我用它来解析原始邮件数据:例如
require 'mail'
maildata = Mail.new(body) #where body is the raw text of an email message
#from there I can see info such as
p maildata.body.decoded #displays the decoded email body
p maildata.from #shows who the email is from
如何确定电子邮件是 plaintext 还是 html 是否有内置方法可以做到这一点?
【问题讨论】:
标签:
ruby
ruby-on-rails-3
email
imap
【解决方案1】:
你可以看看maildata.content_type:
maildata.content_type
#=> "text/plain; charset=us-ascii"
如果是多部分电子邮件,您可以同时使用纯文本和 HTML。然后,您可以查看 parts 数组以查看它包含哪些内容类型:
maildata.content_type
#=> "multipart/alternative; boundary=\"--==_mimepart_4f848491e618f_7e4b6c1f3849940\"; charset=utf-8"
maildata.parts.collect { |part| part.content_type }
#=> ["text/plain; charset=utf-8", "text/html; charset=utf-8"]