Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

UTF8 issue with Net::POP3

by chuntuk (Novice)
on Nov 19, 2012 at 22:55 UTC ( [id://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==?=" <chris@example.com> ; Subject: =? +utf-8?B?VGVzdA==?= From: "=?utf-8?B?Q2hyaXMgSHVudA==?=" <chris@example.com> ; 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1004630]
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found