Bug 28870: Use Email::Address to validate email addresses
[koha-ffzg.git] / Koha / Email.pm
index 450e401..4feeb0a 100644 (file)
@@ -20,8 +20,10 @@ package Koha::Email;
 
 use Modern::Perl;
 
-use Email::Valid;
+use Email::Address;
 use Email::MessageID;
+use List::Util qw( pairs );
+
 use Koha::Exceptions;
 
 use C4::Context;
@@ -76,7 +78,7 @@ sub create {
     my $args = {};
     $args->{from} = $params->{from} || C4::Context->preference('KohaAdminEmailAddress');
     Koha::Exceptions::BadParameter->throw("Invalid 'from' parameter: ".$args->{from})
-        unless Email::Valid->address($args->{from}); # from is mandatory
+        unless $args->{from} =~ m/$Email::Address::mailbox/; # from is mandatory
 
     $args->{subject} = $params->{subject} // '';
 
@@ -88,7 +90,7 @@ sub create {
     }
 
     Koha::Exceptions::BadParameter->throw("Invalid 'to' parameter: ".$args->{to})
-        unless Email::Valid->address($args->{to}); # to is mandatory
+        unless $args->{to} =~ m/$Email::Address::mailbox/; # to is mandatory
 
     my $addresses = {};
     $addresses->{reply_to} = $params->{reply_to};
@@ -106,9 +108,11 @@ sub create {
             if exists $params->{bcc};
     }
 
-    foreach my $address ( keys %{ $addresses } ) {
-        Koha::Exceptions::BadParameter->throw("Invalid '$address' parameter: ".$addresses->{$address})
-            if $addresses->{$address} and !Email::Valid->address($addresses->{$address});
+    foreach my $address ( keys %{$addresses} ) {
+        Koha::Exceptions::BadParameter->throw(
+            "Invalid '$address' parameter: " . $addresses->{$address} )
+          if $addresses->{$address}
+            and $addresses->{$address} !~ m/$Email::Address::mailbox/;
     }
 
     $args->{cc} = $addresses->{cc}
@@ -118,7 +122,7 @@ sub create {
 
     my $email = $self->SUPER::new( $args );
 
-    $email->header( 'ReplyTo', $addresses->{reply_to} )
+    $email->header( 'Reply-To', $addresses->{reply_to} )
         if $addresses->{reply_to};
 
     $email->header( 'Sender'       => $addresses->{sender} ) if $addresses->{sender};
@@ -153,11 +157,11 @@ sub send_or_die {
 
         my @recipients;
 
-        # extract all recipient addresses from header
-        foreach my $header ( 'To', 'Cc', 'Bcc' ) {
-            push @recipients,
-              map { $_->as_string }
-              $self->email->header_obj->header_as_obj($header)->addresses;
+        my @headers = $self->email->header_str_pairs;
+        foreach my $pair ( pairs @headers ) {
+            my ( $header, $value ) = @$pair;
+            push @recipients, split (', ', $value)
+                if grep { $_ eq $header } ('To', 'Cc', 'Bcc');
         }
 
         # Remove the Bcc header