Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

UTF8 issue with Net::POP3

by chuntuk (Novice)
on Nov 19, 2012 at 22:55 UTC ( #1004630=perlquestion: print w/replies, xml ) Need Help??
chuntuk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to read the "from" and "subject" fields from the messages in a POP3 mailbox (I don't care about the content of the messages), and have cobbled together the following code from online examples:

use Net::POP3; # $MAILSERVER,$MAILUSER,$MAILPASS defined here! # Constructors $pop = Net::POP3->new($MAILSERVER); if ($pop->login($MAILUSER, $MAILPASS) > 0) { my $msgnums = $pop->list; # hashref of msgnum => size foreach my $msgnum (keys %$msgnums) { my $head = $pop->top($msgnum,0); my ($subject, $from) = analyze_header($head); print "From: $from ; Subject: $subject \n"; } } $pop->quit; print "done.\n"; sub analyze_header { my $header_array_ref = shift; my $header = join "", @$header_array_ref; my ($subject) = $header =~ /Subject: (.*)/m; my ($from ) = $header =~ /From: (.*)/m; return ($subject, $from); }

It works, insofar as it successfully logs in and reads the (two identical) messages, but this is the output I get:

starting... From: "=?utf-8?B?Q2hyaXMgSHVudA==?=" <> ; Subject: =? +utf-8?B?VGVzdA==?= From: "=?utf-8?B?Q2hyaXMgSHVudA==?=" <> ; Subject: =? +utf-8?B?VGVzdA==?= done.

The contents of the header fields appear (I assume) to be encoded somehow into utf-8, but how do I decode it into something I can make sense of? I assume there must be some standard method of doing this, but none of the documentation I could find gives me any help.

Replies are listed 'Best First'.
Re: UTF8 issue with Net::POP3
by runrig (Abbot) on Nov 19, 2012 at 23:09 UTC
    use Encode (); $subject = Encode::decode('MIME-Header', $subject);

      That's done it - thanks. Wish the docs would mention, somewhere, that you need to do that. Everything seems to assume that once you've retrieved a message, you're done.

        Also, consider using MIME::Head to parse MIME headers.

        To be fair, the module name, Net::POP3, hints that it only does the POP3 protocol. It does not try to parse the headers or MIME attachments or anything. That's some other module's job.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004630]
Front-paged by 2teez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-03-28 09:05 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (328 votes). Check out past polls.