Andrew Newby Andrew Newby - 4 months ago 13
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:

http://search.cpan.org/~markov/MailTools-2.18/lib/Mail/Header.pod

I have tried a couple of ways:

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

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

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

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

and

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

simple.eml

From: example@example.com
To: example2@example.com
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

output

From: example@example.com
To: example2@example.com
Subject: As basic as it gets