Bug 22343: (QA follow-up) Wrap email creation inside the try/catch block
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 1 Oct 2020 14:28:59 +0000 (11:28 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 2 Oct 2020 08:54:41 +0000 (10:54 +0200)
At a later development stage, exceptions where added for bad addresses.
This wasn't addressed in the controllers.

This patch makes the basket and list sending controller scripts move
email creation inside the try/catch block to handle those situations. It
also UTF-8 encodes the attached marc.

On broadly testing this I found that if the TT templates that are used
to build the email contains non-latin characters, those get
double-encoded. So this patch also removes an explicit encoding that is
done, which colides with Email::MIME implicit encoding.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
basket/sendbasket.pl
opac/opac-sendbasket.pl
opac/opac-sendshelf.pl
virtualshelves/sendshelf.pl

index d7e3fbc..9e273b1 100755 (executable)
@@ -30,7 +30,7 @@ use C4::Templates ();
 use Koha::Email;
 use Koha::Token;
 
-my $query = new CGI;
+my $query = CGI->new;
 
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
     {
@@ -114,24 +114,15 @@ if ( $email_add ) {
         $subject = "no subject";
     }
 
-    my $email = Koha::Email->create(
-        {
-            to      => $email_add,
-            subject => $subject,
-        }
-    );
-
     my $email_header = "";
     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
         $email_header = $1;
         $email_header =~ s|\n?(.*)\n?|$1|;
-        $email_header = Encode::encode("UTF-8", $email_header);
     }
 
     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
         $body = $1;
         $body =~ s|\n?(.*)\n?|$1|;
-        $body = Encode::encode("UTF-8", $body);
     }
 
     my $THE_body = <<END_OF_BODY;
@@ -139,15 +130,23 @@ $email_header
 $body
 END_OF_BODY
 
-    $email->text_body( $THE_body );
-    $email->attach(
-        $iso2709,
-        content_type => 'application/octet-stream',
-        name         => 'basket.iso2709',
-        disposition  => 'attachment',
-    );
-
     try {
+
+        my $email = Koha::Email->create(
+            {
+                to      => $email_add,
+                subject => $subject,
+            }
+        );
+
+        $email->text_body( $THE_body );
+        $email->attach(
+            Encode::encode( "UTF-8", $iso2709 ),
+            content_type => 'application/octet-stream',
+            name         => 'basket.iso2709',
+            disposition  => 'attachment',
+        );
+
         my $library = Koha::Patrons->find( $borrowernumber )->library;
         $email->send_or_die({ transport => $library->smtp_server->transport });
         $template->param( SENT => "1" );
index 76dd160..eee5e2c 100755 (executable)
@@ -34,7 +34,7 @@ use Koha::Email;
 use Koha::Patrons;
 use Koha::Token;
 
-my $query = new CGI;
+my $query = CGI->new;
 
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
     {
@@ -127,26 +127,15 @@ if ( $email_add ) {
         $subject = "no subject";
     }
 
-    # if you want to use the KohaAdmin address as from, that is the default no need to set it
-    my $email = Koha::Email->create({
-        to       => $email_add,
-        reply_to => $email_replyto,
-        subject  => $subject,
-    });
-
-    $email->header( 'X-Abuse-Report' => C4::Context->preference('KohaAdminEmailAddress') );
-
     my $email_header = "";
     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
         $email_header = $1;
         $email_header =~ s|\n?(.*)\n?|$1|;
-        $email_header = Encode::encode("UTF-8", $email_header);
     }
 
     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
         $body = $1;
         $body =~ s|\n?(.*)\n?|$1|;
-        $body = Encode::encode("UTF-8", $body);
     }
 
     my $THE_body = <<END_OF_BODY;
@@ -154,20 +143,26 @@ $email_header
 $body
 END_OF_BODY
 
-    $email->text_body( $THE_body );
-    $email->attach(
-        $iso2709,
-        content_type => 'application/octet-stream',
-        name         => 'basket.iso2709',
-        disposition  => 'attachment',
-    );
-
     if ( !defined $iso2709 ) {
         carp "Error sending mail: empty basket";
         $template->param( error => 1 );
     }
     else {
         try {
+            # if you want to use the KohaAdmin address as from, that is the default no need to set it
+            my $email = Koha::Email->create({
+                to       => $email_add,
+                reply_to => $email_replyto,
+                subject  => $subject,
+            });
+            $email->header( 'X-Abuse-Report' => C4::Context->preference('KohaAdminEmailAddress') );
+            $email->text_body( $THE_body );
+            $email->attach(
+                Encode::encode( "UTF-8", $iso2709 ),
+                content_type => 'application/octet-stream',
+                name         => 'basket.iso2709',
+                disposition  => 'attachment',
+            );
             my $library = $patron->library;
             $email->transport( $library->smtp_server->transport );
             $email->send_or_die;
index 44f2bd1..a8ca089 100755 (executable)
@@ -128,24 +128,15 @@ if ( $email ) {
         $subject = "no subject";
     }
 
-    my $email = Koha::Email->create(
-        {
-            to      => $email,
-            subject => $subject,
-        }
-    );
-
     my $email_header = "";
     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
         $email_header = $1;
         $email_header =~ s|\n?(.*)\n?|$1|;
-        $email_header = Encode::encode("UTF-8", $email_header);
     }
 
     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
         $body = $1;
         $body =~ s|\n?(.*)\n?|$1|;
-        $body = Encode::encode("UTF-8", $body);
     }
 
     my $THE_body = <<END_OF_BODY;
@@ -153,15 +144,20 @@ $email_header
 $body
 END_OF_BODY
 
-    $email->text_body( $THE_body );
-    $email->attach(
-        $iso2709,
-        content_type => 'application/octet-stream',
-        name         => 'list.iso2709',
-        disposition  => 'attachment',
-    );
-
     try {
+        my $email = Koha::Email->create(
+            {
+                to      => $email,
+                subject => $subject,
+            }
+        );
+        $email->text_body( $THE_body );
+        $email->attach(
+            Encode::encode( "UTF-8", $iso2709 ),
+            content_type => 'application/octet-stream',
+            name         => 'list.iso2709',
+            disposition  => 'attachment',
+        );
         my $library = Koha::Patrons->find( $borrowernumber )->library;
         $email->transport( $library->smtp_server->transport );
         $email->send_or_die;
index a0da257..f15e2c9 100755 (executable)
@@ -31,7 +31,7 @@ use C4::Output;
 use Koha::Email;
 use Koha::Virtualshelves;
 
-my $query = new CGI;
+my $query = CGI->new;
 
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {
@@ -108,24 +108,15 @@ if ($to_address) {
         $subject = "no subject";
     }
 
-    my $email = Koha::Email->create(
-        {
-            to      => $to_address,
-            subject => $subject,
-        }
-    );
-
     my $email_header = "";
     if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
         $email_header = $1;
         $email_header =~ s|\n?(.*)\n?|$1|;
-        $email_header = Encode::encode("UTF-8", $email_header);
     }
 
     if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
         $body = $1;
         $body =~ s|\n?(.*)\n?|$1|;
-        $body = Encode::encode("UTF-8", $body);
     }
 
     my $THE_body = <<END_OF_BODY;
@@ -133,15 +124,21 @@ $email_header
 $body
 END_OF_BODY
 
-    $email->text_body( $THE_body );
-    $email->attach(
-        $iso2709,
-        content_type => 'application/octet-stream',
-        name         => 'shelf.iso2709',
-        disposition  => 'attachment',
-    );
-
     try {
+        my $email = Koha::Email->create(
+            {
+                to      => $to_address,
+                subject => $subject,
+            }
+        );
+        $email->text_body( $THE_body );
+        $email->attach(
+            Encode::encode("UTF-8", $iso2709),
+            content_type => 'application/octet-stream',
+            name         => 'shelf.iso2709',
+            disposition  => 'attachment',
+        );
+
         my $library = Koha::Patrons->find( $borrowernumber )->library;
         $email->send_or_die({ transport => $library->smtp_server->transport });
         $template->param( SENT => "1" );
@@ -151,7 +148,7 @@ END_OF_BODY
         $template->param( error => 1 );
     };
 
-    $template->param( email => $email );
+    $template->param( email => $to_address );
     output_html_with_http_headers $query, $cookie, $template->output;
 
 }