在 ruby on Rails 中,如何解码内容传输编码的 base64 Mail 对象
我正在 ruby on Rails ActionMailer 上工作。我所做的是,我有一个后台进程轮询我的 Gmail 帐户,并使用“RFC822”协议检索邮件。然后,我将这些邮件发送到 ActionMailer 类中的接收方法。
虽然我可以成功处理几乎所有收到的邮件,但有时我确实会遇到带有英文重音符号的国际邮件。当将这些邮件的正文显示到 STDOUT 时,它们似乎是一些随机字符,如下所示,因为我认为解码是错误的...
这是我在屏幕上打印时邮件对象的正文:
"PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iQ29udGVudC1MYW5ndWFnZSIgY29udGVudD0i
ZW4tdXMiPjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0idGV4dC9odG1s
OyBjaGFyc2V0PVVURi04Ij48bWV0YSBuYW1lPSJHRU5FUkFUT1IiIGNvbnRlbnQ9Ik1pY3Jvc29m
dCBGcm9udFBhZ2UgNC4wIj48bWV0YSBuYW1lPSJQcm9nSWQiIGNvbnRlbnQ9IkZyb250UGFnZS5F
ZGl0b3IuRG9jdW1lbnQiPjx0aXRsZT5OZXcgUGFnZSAxPC90aXRsZT48L2hlYWQ+PGJvZHk+PHRh
YmxlPjx0cj48dGQ+PHA+PGZvbnQgZmFjZT0iQXJpYWwiIGNvbG9yPSJibGFjayIgc2l6ZT0iMSI+
PGI+Q29udGV1ZG8gZW52aWFkbyBkbyBtZXUgQ2VsdWxhciBWaXZvPC9iPjwvZm9udD48L3A+PC90
ZD48L3RyPjx0YWJsZSBib3JkZXI9MD48dHI+PHRkIGNvbHNwYW49MT48aHIgY29sb3I9IjgxODFE
QiI+PC90ZD48L3RyPjx0cj4KPHRyPjx0ZCBjb2xzcGFuPTEgYWxpZ249IkNlbnRlciI+PGZvbnQg
ZmFjZT0iQXJpYWwiIGNvbG9yPSJibGFjayIgc2l6ZT0iMyI+I3IgZm9jbyBkZSBkZW5ndWUgI24g
YnJhc2lsICNzIHNwICNjIHPDo28gcGF1bG8gI2EgcnVhIG1hcmFuaMOjbyBjb20gcnVhIGFyYWNh
anU8L2ZvbnQ+PC90ZD48L3RyPgo8L3RyPjx0cj4KPHRkIGNvbHNwYW49MT4KPGhyIGNvbG9yPSI4
MTgxREIiPgo8L3RkPgo8L3RyPgo8L3RhYmxlPgo8L3RhYmxlPgo8L2JvZHk+CjwvaHRtbD4="
这些国际邮件有内容-传输编码:base64,而不是 7 位。那么我需要做什么才能正确解码电子邮件?我尝试过诸如 Base64.decode64 之类的东西,但它抱怨没有定义解包方法。
我已经为此工作了几个小时但没有运气。有人可以帮我吗?
谢谢
I am working on ruby on rails ActionMailer. What I have done is that I had a background process polling my gmail account and I retrieve mails with 'RFC822' protocol. I then send these mails to a receive method inside my ActionMailer class.
While I can successfully process almost all of the incoming mails, sometimes I do encounter international mails, which have accent marks in english characters. When displaying the bodies of these mails to STDOUT, they appear to be some random characters as follow because I think the decoding is wrong...
This is the body of the mail object when I print it on the screen:
"PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iQ29udGVudC1MYW5ndWFnZSIgY29udGVudD0i
ZW4tdXMiPjxtZXRhIGh0dHAtZXF1aXY9IkNvbnRlbnQtVHlwZSIgY29udGVudD0idGV4dC9odG1s
OyBjaGFyc2V0PVVURi04Ij48bWV0YSBuYW1lPSJHRU5FUkFUT1IiIGNvbnRlbnQ9Ik1pY3Jvc29m
dCBGcm9udFBhZ2UgNC4wIj48bWV0YSBuYW1lPSJQcm9nSWQiIGNvbnRlbnQ9IkZyb250UGFnZS5F
ZGl0b3IuRG9jdW1lbnQiPjx0aXRsZT5OZXcgUGFnZSAxPC90aXRsZT48L2hlYWQ+PGJvZHk+PHRh
YmxlPjx0cj48dGQ+PHA+PGZvbnQgZmFjZT0iQXJpYWwiIGNvbG9yPSJibGFjayIgc2l6ZT0iMSI+
PGI+Q29udGV1ZG8gZW52aWFkbyBkbyBtZXUgQ2VsdWxhciBWaXZvPC9iPjwvZm9udD48L3A+PC90
ZD48L3RyPjx0YWJsZSBib3JkZXI9MD48dHI+PHRkIGNvbHNwYW49MT48aHIgY29sb3I9IjgxODFE
QiI+PC90ZD48L3RyPjx0cj4KPHRyPjx0ZCBjb2xzcGFuPTEgYWxpZ249IkNlbnRlciI+PGZvbnQg
ZmFjZT0iQXJpYWwiIGNvbG9yPSJibGFjayIgc2l6ZT0iMyI+I3IgZm9jbyBkZSBkZW5ndWUgI24g
YnJhc2lsICNzIHNwICNjIHPDo28gcGF1bG8gI2EgcnVhIG1hcmFuaMOjbyBjb20gcnVhIGFyYWNh
anU8L2ZvbnQ+PC90ZD48L3RyPgo8L3RyPjx0cj4KPHRkIGNvbHNwYW49MT4KPGhyIGNvbG9yPSI4
MTgxREIiPgo8L3RkPgo8L3RyPgo8L3RhYmxlPgo8L3RhYmxlPgo8L2JvZHk+CjwvaHRtbD4="
These international mails have Content-Transfer-Encoding:base64 as opposed to 7bit. So what do I need to do so that I can correctly decode the email? I have tried things like Base64.decode64, but it complains that there is no unpack method defined.
I have been working on this for couple hours with no luck. Could anyone please help me?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至少现在 ActionMailer 能够正确解码这些电子邮件。
这应该有效:
At least nowadays ActionMailer is able to correctly decode those emails.
This should work:
看起来您提供给
Base64.decode
的参数不是字符串。 (因为decode
回退到String.unpack
)来自 ruby-doc:
It looks like the parameter you provide to
Base64.decode
is not a string. (Becausedecode
falls back toString.unpack
)From ruby-doc: