From f3f41caa6dd07d18db4e73216e3f58a92d826c3e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 1 Oct 2020 11:28:59 -0300 Subject: [PATCH] Bug 22343: (QA follow-up) Wrap email creation inside the try/catch block 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 Signed-off-by: Jonathan Druart --- basket/sendbasket.pl | 35 +++++++++++++++++------------------ opac/opac-sendbasket.pl | 35 +++++++++++++++-------------------- opac/opac-sendshelf.pl | 30 +++++++++++++----------------- virtualshelves/sendshelf.pl | 35 ++++++++++++++++------------------- 4 files changed, 61 insertions(+), 74 deletions(-) diff --git a/basket/sendbasket.pl b/basket/sendbasket.pl index d7e3fbc74b..9e273b1f45 100755 --- a/basket/sendbasket.pl +++ b/basket/sendbasket.pl @@ -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 =~ /
(.*)/s ) { $email_header = $1; $email_header =~ s|\n?(.*)\n?|$1|; - $email_header = Encode::encode("UTF-8", $email_header); } if ( $template_res =~ /(.*)/s ) { $body = $1; $body =~ s|\n?(.*)\n?|$1|; - $body = Encode::encode("UTF-8", $body); } my $THE_body = <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" ); diff --git a/opac/opac-sendbasket.pl b/opac/opac-sendbasket.pl index 76dd16019c..eee5e2c29b 100755 --- a/opac/opac-sendbasket.pl +++ b/opac/opac-sendbasket.pl @@ -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 =~ /
(.*)/s ) { $email_header = $1; $email_header =~ s|\n?(.*)\n?|$1|; - $email_header = Encode::encode("UTF-8", $email_header); } if ( $template_res =~ /(.*)/s ) { $body = $1; $body =~ s|\n?(.*)\n?|$1|; - $body = Encode::encode("UTF-8", $body); } my $THE_body = <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; diff --git a/opac/opac-sendshelf.pl b/opac/opac-sendshelf.pl index 44f2bd130d..a8ca0899d8 100755 --- a/opac/opac-sendshelf.pl +++ b/opac/opac-sendshelf.pl @@ -128,24 +128,15 @@ if ( $email ) { $subject = "no subject"; } - my $email = Koha::Email->create( - { - to => $email, - subject => $subject, - } - ); - my $email_header = ""; if ( $template_res =~ /
(.*)/s ) { $email_header = $1; $email_header =~ s|\n?(.*)\n?|$1|; - $email_header = Encode::encode("UTF-8", $email_header); } if ( $template_res =~ /(.*)/s ) { $body = $1; $body =~ s|\n?(.*)\n?|$1|; - $body = Encode::encode("UTF-8", $body); } my $THE_body = <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; diff --git a/virtualshelves/sendshelf.pl b/virtualshelves/sendshelf.pl index a0da25768f..f15e2c9f64 100755 --- a/virtualshelves/sendshelf.pl +++ b/virtualshelves/sendshelf.pl @@ -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 =~ /
(.*)/s ) { $email_header = $1; $email_header =~ s|\n?(.*)\n?|$1|; - $email_header = Encode::encode("UTF-8", $email_header); } if ( $template_res =~ /(.*)/s ) { $body = $1; $body =~ s|\n?(.*)\n?|$1|; - $body = Encode::encode("UTF-8", $body); } my $THE_body = <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; } -- 2.39.5