我正在尝试使用 ruby mail gem 阅读电子邮件。
但是 mail.body.decoded
返回的不仅仅是正文消息。如何清理此正文消息并删除不需要的文本,例如:
-20cf30433c9a437cc304939017ef\n内容类型:文本/纯文本;字符集=ISO-8859-1\n内容-
message = $stdin.read
mail = Mail.read_from_string(message)
puts mail.body.decoded
<代码>
--20cf30433c9a437cc304939017ef\n内容类型:文本/纯文本;字符集 = ISO-8859-1\n内容传输编码:可引用打印\n\n 真实正文文本 \\n\n--20cf30433c9a437cc304939017ef\n内容类型:text/html;字符集=ISO-8859-1\n内容传输编码:引用可打印\n\n--20cf30433c9a437cc304939017ef--
如何清理此电子邮件正文邮件消息,仅提取真实正文文本,而不包含任何标头?
我正在创建一个基于 Ruby on Rails 的简单票证系统,当 [电子邮件受保护]。但是,当消息采用 HTML 格式时,主体文本将被标头文本包围。
I'm trying to read an email using ruby mail gem.
But mail.body.decoded
returns me not just the body message. How can I clean up this body message and remove unwanted text like:
-20cf30433c9a437cc304939017ef\nContent-Type: text/plain; charset=ISO-8859-1\nContent-
message = $stdin.read
mail = Mail.read_from_string(message)
puts mail.body.decoded
--20cf30433c9a437cc304939017ef\nContent-Type: text/plain; charset=ISO-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n REAL BODY TEXT \\n\n--20cf30433c9a437cc304939017ef\nContent-Type: text/html; charset=ISO-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n<br clear=3D\"all\">--20cf30433c9a437cc304939017ef--
How can I clean up this email body mail message extracting only the REAL BODY TEXT , without ANY header ?
I'm creating a simple Ticket System based in Ruby on Rails, and a ticket is created when an email is received by [email protected]. But when the message is in HTML format the BODY TEXT is surrounded by HEADERs text.
发布评论
评论(4)
如果您有格式正确的电子邮件,则可以使用邮件帮助程序方法:
如果您有单部分消息(仅限文本)或从整个互联网接收电子邮件,则这并不总是有效,因为您不能依赖每个人的格式那里的客户。相信我,我已经通过惨痛的教训学会了。
If you have a properly formatted email, you can use Mail helper methods:
This doesn't always work if you have e.g. single part messages (text only) or receive email from the internet at large since you can't rely on formatting from every client out there. Believe me, I've learned the hard way.
看起来您有一封多部分电子邮件,因此您可以使用
mail.parts[0].body.decoded
这些可能也会派上用场:
邮件.multipart?
mail.parts.length
github 上的 gem 文档相当不错
looks like you've got a multipart email, so you can use
mail.parts[0].body.decoded
These will probably come in handy too:
mail.multipart?
mail.parts.length
The gem documentation at github is pretty decent
使用
mail
gem,您可以执行以下操作:With the
mail
gem, you can do:添加邮件 gem 并仅使用带有 mail.parts[1].body.decoded 的电子邮件正文格式。
Add the mail gem and just use email body format with mail.parts[1].body.decoded.