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 <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Tomas Cohen Arazi 2020-10-01 11:28:59 -03:00 committed by Jonathan Druart
parent f5def63c34
commit f3f41caa6d
4 changed files with 61 additions and 74 deletions

View File

@ -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" );

View File

@ -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;

View File

@ -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;

View File

@ -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;
}