From db9a80723fc04356c5cbfc5523b7cb49ac116c44 Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Sat, 3 Jun 2023 10:57:59 -0700 Subject: [PATCH] Rework address code to properly catch when multiple address appear As previously written, only the first address was found. The key is that the Email::Address::XS library was misused previously. Based on what is returned by Mail::Header->header_hashref(), it's clear that we should use Email::Address::XS->parse_email_groups() first to extract all addresses first. --- build-label-upi-number.plx | 39 +++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/build-label-upi-number.plx b/build-label-upi-number.plx index 433953c..ffa342b 100755 --- a/build-label-upi-number.plx +++ b/build-label-upi-number.plx @@ -21,7 +21,7 @@ use File::Spec::Functions; use File::Spec; use File::Path qw(make_path); use Mail::Header; -use Email::Address::XS; +use Email::Address::XS qw(parse_email_groups); use File::Copy; use Date::Manip::DM6 qw(ParseDate UnixDate); use Text::CSV; # libtext-csv-perl in Debian @@ -139,18 +139,35 @@ sub ProcessMailDir($$$) { my %parsed = (FromName => '', ToName => '', FromAddr => "", ToAddr => "", CCName => '', CCAddr => '', 'Subject' => '', Date => ''); use Data::Dumper; - foreach my $fieldName (qw/From To CC Subject Date/) { + foreach my $fieldName (qw/From To CC Cc Subject Date/) { foreach my $item (@{$fields->{$fieldName}}) { chomp $item; - if ($fieldName =~ /From|To|CC/) { - my $addr = Email::Address::XS->parse($item); - if ($addr->name ne "") { - $parsed{"${fieldName}Name"} .= "; " if $parsed{"${fieldName}Name"} !~ /^\s*$/; - $parsed{"${fieldName}Name"} .= $addr->name; - } - if ($addr->address ne "") { - $parsed{"${fieldName}Addr"} .= "; " if $parsed{"${fieldName}Addr"} !~ /^\s*$/; - $parsed{"${fieldName}Addr"} .= $addr->address; + if ($fieldName =~ /From|To|CC/i) { + my @groups = parse_email_groups($item); + while ( my($groupName, $addrListRef) = each @groups) { + if (defined $groupName and $groupName !~ /^[01\s*]$/) { + $parsed{"${fieldName}Name"} .= "; " if $parsed{"${fieldName}Name"} !~ /^\s*$/; + $parsed{"${fieldName}Name"} .= $groupName; + } + if (not ref $addrListRef) { + if (defined $addrListRef and $addrListRef !~ /^\s*$/) { + $parsed{"${fieldName}Name"} .= "; " if $parsed{"${fieldName}Name"} !~ /^\s*$/; + $parsed{"${fieldName}Name"} .= $addrListRef; + } + } else { + foreach my $addr (@$addrListRef) { + my $name = $addr->name; + my $address = $addr->address; + if (defined $name and $name !~ /^\s*$/) { + $parsed{"${fieldName}Name"} .= "; " if $parsed{"${fieldName}Name"} !~ /^\s*$/; + $parsed{"${fieldName}Name"} .= $name; + } + if (defined $address and $address !~ /^\s*$/) { + $parsed{"${fieldName}Addr"} .= "; " if $parsed{"${fieldName}Addr"} !~ /^\s*$/; + $parsed{"${fieldName}Addr"} .= $address; + } + } + } } } elsif ($fieldName eq 'Date' and $parsed{Date} =~ /^\s*$/) { $parsed{Date} = ParseDate($item);