使用ruby-mail阅读电子邮件时,无法以文本格式返回邮件正文

4

我正在使用ruby-mail读取一封电子邮件。

除了邮件正文,我得到的所有内容都是正确可读的。

邮件正文显示为其他编码格式。

我的代码如下:

Mail.defaults do
  retriever_method :pop3, :address    => "some.email.com",
              :port       => 995,
              :user_name  => 'domain/username',
              :password   => 'pwd',
              :enable_ssl => true
end


puts "From"
puts mail.from
puts "Sender:"
puts mail.sender
puts "To:"
puts mail.to              
puts "CC:"
puts mail.cc              
puts "Subject:"
puts mail.subject         
puts "Date:"
puts mail.date.to_s       
puts "MessageID:"
puts mail.message_id      
puts "Body:"
#puts mail.body

输出结果为:

输出结果:

发件人: legalholdnotification123@emc.com

寄件人:

收件人: bhavesh.sharma@emc.com

抄送:

主题: case4: Legal Hold Notification

日期: 2012-04-24T14:46:25-04:00

消息ID: 3298720.1335293185423.JavaMail.root@vm-bhaveshok7

正文:

日期:2012年5月5日星期六09:45:08 -0700 Mime版本:1.0 内容类型:纯文本; 字符集=utf-8 内容传输编码:base64 内容ID:<4fa559147a120_138455aab4289ac@USITHEBBASL2C.mail>

Hello Sir/Madam,

Brief introduction to the case case4:
We have identified you as a person who specifically wants to maintain the integrity of documents - with incidents involving personal and electronic documents - that are related to this matter and stored in the management domain. Please carefully review this memorandum document related to preserving evidence policies and guidelines, to ensure that the mandatory document retention policy is followed. [Company] should carefully review this memorandum document related to preserving evidence policies and guidelines, outlining the process for preserving electronic documents. Please contact us immediately if you have any questions or concerns about this matter. [Company] could be subject to legal action if it fails to comply with the mandatory document retention policy and other applicable document retention policies.
Thank you.

我无法阅读邮件正文。

需要做什么才能阅读邮件,我需要从正文中提取文本,并使用邮件正文中存在的链接。

Bhavesh


正文是 base64 编码的。你需要解码它 - Ben
一种方法是将mail.encoded保存为带有.eml扩展名的文件,这样可以在某些电子邮件应用程序(例如Outlook)中打开。这样您就可以查看整个电子邮件,包括附件。 - Gary S. Weaver
3个回答

6

邮件宝石(mail gem)不会自动解码邮件正文。您可以使用以下方法:

mail.message.body.decoded

获取解码后的消息正文。此外,您可能会发现需要访问消息的HTML部分或纯文本部分。为了实现这一点,您可以使用以下类似的代码:

plain_part = message.text_part ? message.text_part.body.decoded : nil
html_part = message.html_part ? message.html_part.body.decoded : nil

您可以使用message.body.decoded作为备选方案,以防这些部分不存在。

0

添加 mail gem,并且仅使用邮件正文格式和 mail.parts[1].body.decoded


0

我使用了:

[...]

ids = imap.search(['UNSEEN'])

ids.each do |msg_id|
  raw_msg = imap.fetch(msg_id,'RFC822').first.attr['RFC822']
  msg = Mail.read_from_string raw_msg
  msg_body_content = msg.multipart?? msg.text_part.body.decoded : message.html_part.body.decoded
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接