Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following header:

From: =?iso-8859-1?Q?Marta_Falc=E3o?= <[email protected]>

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"?

like image 284
James A. Rosen Avatar asked Oct 26 '25 10:10

James A. Rosen


2 Answers

Using the newer Mail gem:

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

like image 168
Omri Sivan Avatar answered Oct 29 '25 08:10

Omri Sivan


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
like image 30
2 revsJames A. Rosen Avatar answered Oct 29 '25 09:10

2 revsJames A. Rosen