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:
It works, insofar as it successfully logs in and reads the (two identical) messages, but this is the output I get:
The contents of the header fields appear 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.
-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
Code:
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);
}
Code:
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.
-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd