Search code examples
perlimap

IMAP Client processing emails using perl


I am using the following to connect to a mail server and get the subject and body of the emails using the code below.

my $subject = $imap->subject($msg) or do { print "Error obtaining Subject of $msg : $@";  next; };

I get output like below. Do we know how to convert the output into human readable format. I tried using the decode to base64 with out any luck. Please help.

=?utf-8?B?UkU6IFJlYXNzaWduZWTCoHzCoFByaW9yaXR5OsKgTG93wqB8IEluY2lkZW50?= =?utf-8?B?OsKgSU5DMDAwMDAwODQxMjc4wqA=?=


Solution

  • Non-ASCII header fields in mails are encoded with RFC 2047. It can be decoded with Encode::MIME::Header:

    use Encode 'decode';
    print decode('MIME-Header',
        '=?utf-8?B?UkU6IFJlYXNzaWduZWTCoHzCoFByaW9yaXR5OsKgTG93wqB8IEluY2lkZW50?=
         =?utf-8?B?OsKgSU5DMDAwMDAwODQxMjc4wqA=?=');