my $mail = Email::Simple->new($msg); my (%headers_i_care_about); foreach my $i_care_about (qw(Received Date From To Subject Return-Path)) { my @{$headers_i_care_about{$i_care_about}} = $mail->header($i_care_about); # or, if you want it as a single string, something like: # my $headers_i_care_about{$i_care_about} = # join(' ', $mail->header($i_care_about)); } #### my (%headers); my ($lastheader); foreach my $line (@msg) { last if ($line =~ m/^\s*$/); # Reached end of headers if ($line =~ m/^\s+(.+)/) { $headers{$lastheader} .= $1; } else { my @parts = split(/:/, $line, 2); $headers{$parts[0]} = $parts[1]; $lastheader = $parts[0]; } }