How to decode an RFC 2047 encoded email header in Ruby?

白昼怎懂夜的黑 提交于 2019-12-20 02:12:22

问题


I have the following header:

From: =?iso-8859-1?Q?Marta_Falc=E3o?= <marta.falcao@example.com.br>

I can easily split out the stuff before the <, which leaves me with

"=?iso-8859-1?Q?Marta_Falc=E3o?="

What can I use to turn this into "Marta Falcão"?


回答1:


Using the newer Mail gem:

Mail::Encodings.value_decode(str) or Mail::Encodings.unquote_and_convert_to(str, to_encoding)




回答2:


Thanks to Roland Illig for his comment, which led me to two options:

  1. install rfc2047-ruby and call Rfc2047.decode(header)
  2. install TMail and call TMail::Unquoter.unquote_and_convert_to(header, 'utf-8') or better yet TMail::Address.parse(header).friendly, the latter of which strips out the <email address> part


来源:https://stackoverflow.com/questions/7488875/how-to-decode-an-rfc-2047-encoded-email-header-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!