From f1dd87f07488aeb90e6f33ea0312427474a1df2b Mon Sep 17 00:00:00 2001
From: theminh <theminh@inlibro.com>
Date: Thu, 24 Feb 2022 15:48:04 -0500
Subject: [PATCH] Bug 12532: Send email to guarantee and guarantor;

---
 C4/Letters.pm  |  5 +++++
 Koha/Email.pm  | 17 ++++++++++++-----
 Koha/Patron.pm | 22 ++++++++++++----------
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/C4/Letters.pm b/C4/Letters.pm
index 0497664827..366e313274 100644
--- a/C4/Letters.pm
+++ b/C4/Letters.pm
@@ -1471,6 +1471,11 @@ sub _send_message_by_email {
       if !$message->{to_address}
       || $message->{to_address} ne $email->email->header('To');
 
+    _update_message_from_address($message->{'message_id'},$email->email->header('From') )
+      if !$message->{from_address}
+      || $message->{from_address} ne $email->email->header('From');
+
+
     try {
         $email->send_or_die({ transport => $smtp_server->transport });
 
diff --git a/Koha/Email.pm b/Koha/Email.pm
index 090f475938..6f6b69f979 100644
--- a/Koha/Email.pm
+++ b/Koha/Email.pm
@@ -118,11 +118,18 @@ sub create {
         $args->{to} = $params->{to};
     }
 
-    Koha::Exceptions::BadParameter->throw(
-        error     => "Invalid 'to' parameter: " . $args->{to},
-        parameter => 'to'
-    ) unless Koha::Email->is_valid( $args->{to} );    # to is mandatory
-
+    #    Koha::Exceptions::BadParameter->throw(
+    #        error     => "Invalid 'to' parameter: " . $args->{to},
+    #        parameter => 'to'
+    #    ) unless Koha::Email->is_valid( $args->{to} );    # to is mandatory
+    my @emails = split(',', $args->{to});
+    foreach my $email (@emails) {
+       $email =~ s/ //g;
+       Koha::Exceptions::BadParameter->throw(
+           error     => "Invalid 'to' parameter: ".$email,
+	   parameter => 'to'
+       ) unless Koha::Email->is_valid($email);
+    }
     my $addresses = {};
     $addresses->{reply_to} = $params->{reply_to};
     $addresses->{reply_to} ||= C4::Context->preference('ReplytoDefault')
diff --git a/Koha/Patron.pm b/Koha/Patron.pm
index ea0c570146..c80a040609 100755
--- a/Koha/Patron.pm
+++ b/Koha/Patron.pm
@@ -1425,17 +1425,19 @@ sub notice_email_address{
 
     my $use_guarantor = C4::Context->preference('RedirectGuaranteeEmail');
     if ($use_guarantor) {
-        my $guarantor = $self->guarantor;
-        if ($guarantor) {
-            if ( $which_address eq 'OFF' ) {
-                $guarantor_address = $guarantor->first_valid_email_address;
-            } else {
-                $guarantor_address = $guarantor->$which_address || '';
-            }
-            if ($address){
-                $address .= ', ';
-            }
+        my @guarantors = map { $_->guarantors->as_list } $self->guarantor_relationships();
+        if (@guarantors) {
+            foreach my $guarantor (@guarantors) {
+                if ( $which_address eq 'OFF' ) {
+                    $guarantor_address = $guarantor->first_valid_email_address;
+                } else {
+                    $guarantor_address = $guarantor->$which_address || '';
+                }
+                if ($address){
+                    $address .= ', ';
+                }
                 $address .=  $guarantor_address if $guarantor_address;
+            }
         }
     }
     return $address;
-- 
2.34.1