Search code examples
perlmime

need help in parsing mime subject perl


I managed to get emails from gmail with their subject contains UTF-8 characters ,

Subject: =?utf-8?B?5L2g5aW9IOS9oOWlvQ==?=

I searched the interent I found its encoded quoted-printable

I tried using the shown code to decode the subject

use MIME::QuotedPrint;

print decode_qp("?utf-8?B?5L2g5aW9IOS9oOWlvQ==?=");

but it prints the same message , I also tried removing ?utf-8? but no use, so can one help me in converting the above subject to utf-8 characters instead of the encoding above


Solution

  • Use the Encode::MIME::Header module, as in

    $ perl -MEncode -le 'print Encode::encode("utf8", \
      Encode::decode("MIME-Header", "=?utf-8?B?5L2g5aW9IOS9oOWlvQ==?="))'
    你好 你好
    

    or

    #! /usr/bin/env perl
    
    use v5.10.0;
    use strict;
    use warnings;
    
    use Encode qw/ decode /;
    
    my $subject = "=?utf-8?B?5L2g5aW9IOS9oOWlvQ==?=";
    
    binmode STDOUT, ":encoding(UTF-8)";
    say decode "MIME-Header", $subject;