Search code examples
perlemailmime

Trouble with perl Email::MIME


I'm having trouble using the Email:MIME module in perl. It's probably because I'm using it wrong, but finding examples for using it is difficult. I'm pretty sure I'm supposed to be using a scalar of the full message as an input, but it's not working. Here is my code and my output

Code:

#!/usr/bin/perl

use Net::POP3;
use Email::MIME;
local $| = 1;

my $pop = Net::POP3->new('pop.mail.server');

print "Logging in....";
if ($pop->login('username','password')) {
 print "logged in successfully\n";
 my $msgs = $pop->list;
 my @keys = keys(%$msgs);
 my $msgr = $pop->get($keys[1]); #Selects a more or less random email for testing
 my $msg = join("",@$msgr);

 my $parsed = Email::MIME->new($msg);
 foreach my $key (keys %$parsed) {print $key.":".$parsed{$key}."\n";}
}

Output:

Logging in....logged in successfully
body:
mycrlf:
body_raw:
parts:
ct:
header:

Solution

  • Always use strict; and use warnings;. This will immediately point out an error:

    Global symbol "%parsed" requires explicit package name at p line 21.

    Line 21 should be:

    foreach my $key (keys %$parsed) {print $key.":".$parsed->{$key}."\n";}