Ruby,如何使用 MIME 多部分正确解码邮件?
我正在尝试编写一个可以从 gmail 中提取邮件并用 Ruby 获取内容的系统。 (使用 imap 或 pop)
据我所知,有 'ruby-gmail'、'mail' (较新的tmail 版本)和“action mailer”可能会帮助我做到这一点。
我现在正在尝试“mail”和“ruby-gmail”,我使用这样的解码函数:
gmail.inbox.emails[0].body.decoded
但是有些邮件可以正确解码,但有些则不能。
解码邮件的输出如下所示:
This is MIME multipart 6.
--__=_Part_Boundary_002_310696381.907173471
Content-Type: text/plain;
charset="big5"
Content-Transfer-Encoding: quoted-printable
=AE=BC=A5=BF=A7A=A6n,
.......(some encoded content)
对于其他一些邮件,内容传输编码是 base64。
有没有更好的方法来正确解码邮件?
或者我只需要阅读邮件,获取编码部分, 并使用 Base64.decode64 或 unpack.("M") 来解码邮件?
I'm trying to write a system which could pull mails from gmail and get the content in Ruby. (using imap or pop)
And as far as I know, there are 'ruby-gmail', 'mail' (the newer version of tmail) and 'action mailer' that might help me to do this.
I'm now trying 'mail' and 'ruby-gmail', and I use the decoded function like this:
gmail.inbox.emails[0].body.decoded
But some mails could be correctly decoded, but some couldn't.
The output of the decoded mail looks like this:
This is MIME multipart 6.
--__=_Part_Boundary_002_310696381.907173471
Content-Type: text/plain;
charset="big5"
Content-Transfer-Encoding: quoted-printable
=AE=BC=A5=BF=A7A=A6n,
.......(some encoded content)
And to some other mails, the Content-Transfer-Encoding are base64.
Is there any better way to correctly decode the mails?
Or I just need to read into the mail, get the encoded part,
and use Base64.decode64 or unpack.("M") to decode the mail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道“gmail”gem,但“mail”gem 效果很好。类似的东西
应该可以工作(对于其他部分使用'n'而不是0)
还要注意它可能是一个附件,所以你需要
mail.attachments[0].decoded
I don't know about 'gmail' gem, but 'mail' one works pretty well. Something like
should work (use 'n' instead of 0 for other parts)
Also be aware that it could be an attachment, so you'd need
mail.attachments[0].decoded