将电子邮件中的文本/纯内容转换为普通文本
当我尝试从我的 IMAP 帐户读取一些电子邮件时:
imap.search(['NOT','SEEN']).each do |message_id|
mail = imap.fetch(message_id, "BODY[1]")
end
我收到各种 ascii?我的字符串中的代码,例如 =20 =93 =94 等。 已经尝试了很多转换或解码的方法,但没有成功。我怎样才能摆脱这些代码?
When i try to read some email from my IMAP account:
imap.search(['NOT','SEEN']).each do |message_id|
mail = imap.fetch(message_id, "BODY[1]")
end
i get all kinds of ascii?? codes in my string, like =20 =93 =94 etc.
Tried already lots of things to convert or decode, but no success. How can i get rid of these codes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有许多不同的选项可用于对消息正文进行编码,例如quoted-printable、base-64 等。在 Ruby 中最简单的事情是将整个消息传递到 mail gem,让它进行解析,然后输出纯文本内容。
根据经验,您可能实际上会发现您需要执行类似以下操作:
当我们将消息发送到 CloudMailin 中的应用程序时,我们会使用与此类似的内容,以确保我们找到明文部分,因为并不总是保证明文部分部分将在正文中而不是 mime 编码。
There are a number of different options for encoding the message body such as quoted-printable, base-64 and so on. The easiest thing to do in Ruby is to pass the whole message into the mail gem, let it do the parsing and then output the plain text content.
In experience you might actually find that you need to do something like the following:
We use something similar to this when we send the message to an app in CloudMailin in order to make sure we find the plain part as it's not always guaranteed that the plain part will be in the body and not mime encoded.
我必须使用类似以下内容来解析电子邮件。
我解析的其中一条消息根本不包含 text_part,因此在这种情况下将 nil 放入其中将不起作用。
I had to use something like the following to parse out the email.
One of the messages I parsed did not contain text_part at all, so putting a nil in there won't work in that case.
听起来您好像找到了 Quoted-printable 正文。您应该查找正文的编码是什么,并相应地解析它。看起来像 Net::IMAP: :BodyTypeBasic 可以为您提供此信息,但恐怕我不知道足够的 ruby 来帮助您进一步了解。
Sounds like you have found a Quoted-printable body. You should look up what the encoding is for the body, and parse it accordingly. Seems like Net::IMAP::BodyTypeBasic can give you this information, but I'm afraid I don't know enough ruby to get you any further.