From a9b364a58811284f950d645fab9e6a7f6293eb08 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 5 Aug 2021 08:00:19 +0100 Subject: [PATCH] Bug 28803: Add invalid email handling in _send_message_by_email MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This patch adds a try/catch block around the call to Koha::Email->create to catch and handle invalid emails being passed in the parameters. The message is marked as 'failed' with an error_code of 'INVALID_EMAIL'. Signed-off-by: Kyle M Hall Signed-off-by: Joonas Kylmälä Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- C4/Letters.pm | 41 ++++++++++++------- .../prog/en/modules/members/notices.tt | 1 + 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index e256d6435a..d07226335c 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1400,20 +1400,33 @@ sub _send_message_by_email { ); return; }; - my $email = Koha::Email->create( - { - to => $to_address, - ( - C4::Context->preference('NoticeBcc') - ? ( bcc => C4::Context->preference('NoticeBcc') ) - : () - ), - from => $from_address, - reply_to => $message->{'reply_address'} || $branch_replyto, - sender => $branch_returnpath, - subject => "" . $message->{subject} - } - ); + my $email = try { + Koha::Email->create( + { + to => $to_address, + ( + C4::Context->preference('NoticeBcc') + ? ( bcc => C4::Context->preference('NoticeBcc') ) + : () + ), + from => $from_address, + reply_to => $message->{'reply_address'} || $branch_replyto, + sender => $branch_returnpath, + subject => "" . $message->{subject} + } + ); + } + catch { + _set_message_status( + { + message_id => $message->{'message_id'}, + status => 'failed', + failure_code => 'INVALID_EMAIL' + } + ); + return 0; + }; + return unless $email; if ( $is_html ) { $email->html_body( diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt index 31bd2dbb6f..5503654b66 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/notices.tt @@ -95,6 +95,7 @@ [% IF ( QUEUED_MESSAGE.failure_code ) %] [% IF ( QUEUED_MESSAGE.failure_code == "INVALID_BORNUMBER" ) %]Invalid borrowernumber [% borrowernumber | html %] [% ELSIF ( QUEUED_MESSAGE.failure_code == 'NO_EMAIL' ) %]Unable to find an email address for this borrower + [% ELSIF ( QUEUED_MESSAGE.failure_code == "INVALID_EMAIL" ) %]Invalid email address found [% borrowernumber | html %] [% ELSIF ( QUEUED_MESSAGE.failure_code == 'NO_FROM' ) %]Missing from email address [% ELSIF ( QUEUED_MESSAGE.failure_code == 'MISSING_SMS' ) %]Missing SMS number [% ELSIF ( QUEUED_MESSAGE.failure_code == 'DUPLICATE_MESSAGE' ) %]Message is duplicate -- 2.39.5