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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.