@@ -781,9 +781,11 @@ sub expand_one_alias {
781
781
}
782
782
783
783
@initial_to = expand_aliases(@initial_to );
784
- @initial_to = ( map { sanitize_address( $_ ) } @initial_to );
784
+ @initial_to = validate_address_list(sanitize_address_list( @initial_to ) );
785
785
@initial_cc = expand_aliases(@initial_cc );
786
+ @initial_cc = validate_address_list(sanitize_address_list(@initial_cc ));
786
787
@bcclist = expand_aliases(@bcclist );
788
+ @bcclist = validate_address_list(sanitize_address_list(@bcclist ));
787
789
788
790
if ($thread && !defined $initial_reply_to && $prompting ) {
789
791
$initial_reply_to = ask(
@@ -826,12 +828,45 @@ sub extract_valid_address {
826
828
$address =~ s / ^\s *<(.*)>\s *$/ $1 / ;
827
829
if ($have_email_valid ) {
828
830
return scalar Email::Valid-> address($address );
829
- } else {
830
- # less robust/correct than the monster regexp in Email::Valid,
831
- # but still does a 99% job, and one less dependency
832
- $address =~ / ($local_part_regexp \@ $domain_regexp )/ ;
833
- return $1 ;
834
831
}
832
+
833
+ # less robust/correct than the monster regexp in Email::Valid,
834
+ # but still does a 99% job, and one less dependency
835
+ return $1 if $address =~ / ($local_part_regexp \@ $domain_regexp )/ ;
836
+ return undef ;
837
+ }
838
+
839
+ sub extract_valid_address_or_die {
840
+ my $address = shift ;
841
+ $address = extract_valid_address($address );
842
+ die " error: unable to extract a valid address from: $address \n "
843
+ if !$address ;
844
+ return $address ;
845
+ }
846
+
847
+ sub validate_address {
848
+ my $address = shift ;
849
+ while (!extract_valid_address($address )) {
850
+ print STDERR " error: unable to extract a valid address from: $address \n " ;
851
+ $_ = ask(" What to do with this address? ([q]uit|[d]rop|[e]dit): " ,
852
+ valid_re => qr / ^(?:quit|q|drop|d|edit|e)/ i ,
853
+ default => ' q' );
854
+ if (/ ^d/i ) {
855
+ return undef ;
856
+ } elsif (/ ^q/i ) {
857
+ cleanup_compose_files();
858
+ exit (0);
859
+ }
860
+ $address = ask(" Who should the email be sent to (if any)? " ,
861
+ default => " " ,
862
+ valid_re => qr /\@ .*\. / , confirm_only => 1);
863
+ }
864
+ return $address ;
865
+ }
866
+
867
+ sub validate_address_list {
868
+ return (grep { defined $_ }
869
+ map { validate_address($_ ) } @_ );
835
870
}
836
871
837
872
# Usually don't need to change anything below here.
@@ -919,6 +954,10 @@ sub quote_subject {
919
954
# use the simplest quoting being able to handle the recipient
920
955
sub sanitize_address {
921
956
my ($recipient ) = @_ ;
957
+
958
+ # remove garbage after email address
959
+ $recipient =~ s / (.*>).*$/ $1 / ;
960
+
922
961
my ($recipient_name , $recipient_addr ) = ($recipient =~ / ^(.*?)\s *(<.*)/ );
923
962
924
963
if (not $recipient_name ) {
@@ -946,6 +985,10 @@ sub sanitize_address {
946
985
947
986
}
948
987
988
+ sub sanitize_address_list {
989
+ return (map { sanitize_address($_ ) } @_ );
990
+ }
991
+
949
992
# Returns the local Fully Qualified Domain Name (FQDN) if available.
950
993
#
951
994
# Tightly configured MTAa require that a caller sends a real DNS
@@ -1008,14 +1051,13 @@ sub maildomain {
1008
1051
1009
1052
sub send_message {
1010
1053
my @recipients = unique_email_list(@to );
1011
- @cc = (grep { my $cc = extract_valid_address ($_ );
1054
+ @cc = (grep { my $cc = extract_valid_address_or_die ($_ );
1012
1055
not grep { $cc eq $_ || $_ =~ / <\Q ${cc} \E >$ / } @recipients
1013
1056
}
1014
- map { sanitize_address($_ ) }
1015
1057
@cc );
1016
1058
my $to = join (" ,\n\t " , @recipients );
1017
1059
@recipients = unique_email_list(@recipients ,@cc ,@bcclist );
1018
- @recipients = (map { extract_valid_address ($_ ) } @recipients );
1060
+ @recipients = (map { extract_valid_address_or_die ($_ ) } @recipients );
1019
1061
my $date = format_2822_time($time ++);
1020
1062
my $gitversion = ' @@GIT_VERSION@@' ;
1021
1063
if ($gitversion =~ m / ..GIT_VERSION../ ) {
@@ -1258,7 +1300,7 @@ sub send_message {
1258
1300
foreach my $addr (parse_address_line($1 )) {
1259
1301
printf (" (mbox) Adding to: %s from line '%s '\n " ,
1260
1302
$addr , $_ ) unless $quiet ;
1261
- push @to , sanitize_address( $addr ) ;
1303
+ push @to , $addr ;
1262
1304
}
1263
1305
}
1264
1306
elsif (/ ^Cc:\s +(.*)$ / ) {
@@ -1367,6 +1409,9 @@ sub send_message {
1367
1409
($confirm =~ / ^(?:auto|compose)$ / && $compose && $message_num == 1));
1368
1410
$needs_confirm = " inform" if ($needs_confirm && $confirm_unconfigured && @cc );
1369
1411
1412
+ @to = validate_address_list(sanitize_address_list(@to ));
1413
+ @cc = validate_address_list(sanitize_address_list(@cc ));
1414
+
1370
1415
@to = (@initial_to , @to );
1371
1416
@cc = (@initial_cc , @cc );
1372
1417
@@ -1422,14 +1467,10 @@ sub unique_email_list {
1422
1467
my @emails ;
1423
1468
1424
1469
foreach my $entry (@_ ) {
1425
- if (my $clean = extract_valid_address($entry )) {
1426
- $seen {$clean } ||= 0;
1427
- next if $seen {$clean }++;
1428
- push @emails , $entry ;
1429
- } else {
1430
- print STDERR " W: unable to extract a valid address" ,
1431
- " from: $entry \n " ;
1432
- }
1470
+ my $clean = extract_valid_address_or_die($entry );
1471
+ $seen {$clean } ||= 0;
1472
+ next if $seen {$clean }++;
1473
+ push @emails , $entry ;
1433
1474
}
1434
1475
return @emails ;
1435
1476
}
0 commit comments