Andrew Newby Andrew Newby - 1 year ago 78
Perl Question

Mail::Header - can't get it to show me the headers

I'm trying to work out how to use this Perl module:

I have tried a couple of ways:

my $fh = open (READIT, "./test.eml");

use Mail::Header;
my $head = Mail::Header->new();

print Dumper($head);

All that does though, is print out some empty values:

$VAR1 = bless( {
'mail_hdr_foldlen' => 79,
'mail_hdr_modify' => 0,
'mail_hdr_list' => [],
'mail_hdr_hash' => {},
'mail_hdr_mail_from' => 'KEEP',
'mail_hdr_lengths' => {}
}, 'Mail::Header' );

I've also tried it using the email as a string, in $message:

my $head = Mail::Header->new( $message );

Same problem though.

I'm a bit confused as to what I'm doing wrong. Can anyone suggest something to try?

Answer Source

The problem is that you have opened your .eml file using the READIT handle, but you are then reading from file handle $fh to populate the Mail::Header object. You will also have received the warning messages

Name "main::READIT" used only once: possible typo


Name "main::fh" used only once: possible typo

which you really should have reported

This program works fine

use strict;
use warnings 'all';

use Mail::Header;

open my $fh, '<', 'Support Account Password Assistance.eml' or die $!;
my $head = Mail::Header->new($fh);
print $head->as_string;

When used with this file for input


Subject: As basic as it gets

This is the plain text body of the message.  Note the blank line
between the header information and the body of the message.

It generates this output


Subject: As basic as it gets
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download