邮件宝石-如何清理正文字符串

发布于 2024-09-29 03:43:49 字数 847 浏览 0 评论 0 原文

我正在尝试使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的呼吸 2024-10-06 03:43:49

如果您有格式正确的电子邮件,则可以使用邮件帮助程序方法:

mail = Mail.new(email_string)
mail.text_part # finds the first text/plain part
mail.html_part # finds the first text/html part

如果您有单部分消息(仅限文本)或从整个互联网接收电子邮件,则这并不总是有效,因为您不能依赖每个人的格式那里的客户。相信我,我已经通过惨痛的教训学会了。

If you have a properly formatted email, you can use Mail helper methods:

mail = Mail.new(email_string)
mail.text_part # finds the first text/plain part
mail.html_part # finds the first text/html part

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.

兲鉂ぱ嘚淚 2024-10-06 03:43:49

看起来您有一封多部分电子邮件,因此您可以使用
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

紫轩蝶泪 2024-10-06 03:43:49

使用 mail gem,您可以执行以下操作:

text = mail.multipart? ? mail.text_part.decoded : mail.body.decoded`

With the mail gem, you can do:

text = mail.multipart? ? mail.text_part.decoded : mail.body.decoded`
幸福不弃 2024-10-06 03:43:49

添加邮件 gem 并仅使用带有 mail.parts[1].body.decoded 的电子邮件正文格式。

Add the mail gem and just use email body format with mail.parts[1].body.decoded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文