Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Module for decoding utf-8 imap headers?

by nysus (Parson)
on May 30, 2020 at 16:45 UTC ( [id://11117506]=perlquestion: print w/replies, xml ) Need Help??

nysus has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the Mail::IMAPClient module to fetch emails from my inbox. It works well, but after getting a good way through writing a wrapper for it, I discovered that it doesn't seem to handle UTF-8 encoded email headers that look something like this:

=?UTF-8?B?VHJ1ZUJsdWU=?= <jetblueairways@email.jetblue.com>

This example is a "From" header.

Maybe it's because the module didn't know about such things in 2003. At any rate, I tried the Encode::Simple module like this to try to clean things up:

$sender =~ s/^=\?utf-8//i; $sender =~ s/\?=//i; use Encode::Simple qw (decode_utf8); $sender = decode_utf8($sender);

My approach did not work, however. The string was unchanged. Further confusing me is that some headers look more like url encoding than utf8 encoding:

=?utf-8?Q?rePurpose=20Global?= <hello@repurpose.global>

So how do I properly handle these encoded headers?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Module for decoding utf-8 imap headers?
by afoken (Chancellor) on May 30, 2020 at 19:51 UTC
    =?UTF-8?B?VHJ1ZUJsdWU=?= <jetblueairways@email.jetblue.com>

    Encode::MIME::Header should do the trick:

    >perl -MEncode=decode -MEncode::MIME::Header -E 'say decode("MIME-Head +er", "=?UTF-8?B?VHJ1ZUJsdWU=?=")' TrueBlue >

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Module for decoding utf-8 imap headers?
by nysus (Parson) on May 30, 2020 at 17:01 UTC

    OK, some more googling revealed that that encoding method is called "MIME words". That led me to the MIME::Words module. This works:

    use MIME::Words qw (:all); if ($sender =~ /^=\?UTF/i) { $sender = decode_mimewords($sender); }

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11117506]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found