邮件宝石 - 如何清理身体的字符串 [英] Mail gem - how to clean up the body string

查看:134
本文介绍了邮件宝石 - 如何清理身体的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ruby邮件宝石阅读电子邮件。
但是 mail.body.decoded 不仅返回正文消息。如何清理此正文消息并删除不需要的文本,如:

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\\\
Content-Type:text / plain ; charset = ISO-8859-1\\\
Content-

message = $stdin.read
mail = Mail.read_from_string(message)
puts mail.body.decoded


--20cf30433c9a437cc304939017ef\\\
Content-Type:text / plain; charset = ISO-8859-1\\\
Content-Transfer-Encoding:quoted-printable\\\
n\\\
REAL BODY TEXT \\\\
\\\
- 20cf30433c9a437cc304939017ef\\\
Content-Type:text / html; charset = ISO-8859-1\\\
Content-Transfer-Encoding:quoted-printable\\\
n\\\
< br clear = 3D\all\> - 20cf30433c9a437cc304939017ef--

如何清理此电子邮件正文邮件消息,仅提取REAL BODY TEXT,没有任何标题?

How can I clean up this email body mail message extracting only the REAL BODY TEXT , without ANY header ?

我正在创建一个基于Ruby on Rails的简单的Ticket系统,并在ticket@mydomain.com收到一封电子邮件时创建一张机票。但是当消息是HTML格式时,BODY TEXT被HEADER文本包围。

I'm creating a simple Ticket System based in Ruby on Rails, and a ticket is created when an email is received by ticket@mydomain.com. But when the message is in HTML format the BODY TEXT is surrounded by HEADERs text.

推荐答案

如果您的格式正确,您可以使用邮件助手方法:

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.

这篇关于邮件宝石 - 如何清理身体的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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