in reply to Parsing Pegasus email boxes
And the output is#!C:/Perl/bin/Perl.exe -w use strict; use Data::Dumper; my %MailList; # A list of all seen addresses. # This hash is setup in step 1 in the # previous post. # Parse the mail box while (<DATA>) { next unless /^From:\s/; # ignore all other lines chomp; # except the from line # assume that email addresses are separated # by the ',' character. my @emails = split /,\s*/, substr($_, 6); # build a hash from the email address list my %emails = map { s/\s+$//g; # remove tailing spaces my ($name, $address); if (/>$/) { # look for < ... > ($name, $address) = /^(.*)\s<(.*)>$/; } else { $name = $address = $_; } $address => $name } @emails; # inspect captured email details # print Dumper(\%emails); # check if the email address(es) are already seen foreach (keys %emails) { if (! exists $MailList{$_}) { # ok, we haven't seen this Email address yet $MailList{$_} = $emails{$_}; } } } print Data::Dumper->Dump([\%MailList], ['MailList']); __DATA__ From: Roger <roger@somewhere.com>, Roger 2 <roger2@nowhere.com> To: Peter R <peterr@us.com> Subject: Hello Hello Peter, this from address has two addresses From: roger3@somewhere.com To: Peter R <peterr@us.com> Subject: Hello Hello Peter, this from address has no full name From: roger3@somewhere.com To: Peter R <peterr@us.com> Subject: Hello Hello Peter, has already seen this from address
The code is able to handle Email address in the forms of:$MailList = { 'roger2@nowhere.com' => 'Roger 2', 'roger3@somewhere.com' => 'roger3@somewhere.com', 'roger@somewhere.com' => 'Roger' };
You can tweak it a bit if the format of the Mail Addresses are different, or have a address separator other than ','. Oh yes, and put it in a subroutine and add more error checking for a good coding practise.Full Name <email.address@somewhere.com> Email.Address@somewhere.com
|
|---|