From 48a27df14be01e279e2a598e7cb75fb4d3a8ab15 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 10 Jul 2013 10:01:59 +0200 Subject: [PATCH] Bug 5342: Catch errors raise by sendmail For example, Mail::Sendmail can raise an error if no 'From' address if defined. --- C4/Letters.pm | 20 +++++++++++++++----- C4/Serials.pm | 7 ++++++- acqui/lateorders.pl | 2 +- .../prog/en/modules/serials/claims.tt | 6 ++++-- members/memberentry.pl | 2 +- serials/claims.pl | 8 ++++++-- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index da17cb4..a223708 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -309,7 +309,10 @@ sub SendAlerts { Message => Encode::encode( "utf8", "" . $letter->{content} ), 'Content-Type' => 'text/plain; charset="utf8"', ); - sendmail(%mail) or carp $Mail::Sendmail::error; + unless ( sendmail(%mail) ) { + carp $Mail::Sendmail::error; + return { error => $Mail::Sendmail::error }; + } } } elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' ) { @@ -351,7 +354,7 @@ sub SendAlerts { push @email, $databookseller->{contemail} if $databookseller->{contemail}; unless (@email) { warn "Bookseller $dataorders->[0]->{booksellerid} without emails"; - return { error => "no_email" }; + return { error => "no_to_address" }; } my $userenv = C4::Context->userenv; @@ -375,7 +378,11 @@ sub SendAlerts { Message => Encode::encode( "utf8", "" . $letter->{content} ), 'Content-Type' => 'text/plain; charset="utf8"', ); - sendmail(%mail) or carp $Mail::Sendmail::error; + + unless ( sendmail(%mail) ) { + carp $Mail::Sendmail::error; + return { error => $Mail::Sendmail::error }; + } logaction( "ACQUISITION", @@ -404,7 +411,7 @@ sub SendAlerts { want_librarian => 1, ) or return; - return { error => "no_email" } unless $externalid->{'emailaddr'}; + return { error => "no_to_address" } unless $externalid->{'emailaddr'}; my %mail = ( To => $externalid->{'emailaddr'}, From => $branchdetails->{'branchemail'} || C4::Context->preference("KohaAdminEmailAddress"), @@ -412,7 +419,10 @@ sub SendAlerts { Message => Encode::encode( "utf8", $letter->{'content'} ), 'Content-Type' => 'text/plain; charset="utf8"', ); - sendmail(%mail) or carp $Mail::Sendmail::error; + unless ( sendmail(%mail) ) { + carp $Mail::Sendmail::error; + return { error => $Mail::Sendmail::error }; + } } } diff --git a/C4/Serials.pm b/C4/Serials.pm index 377f6b4..d649f5c 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -1182,7 +1182,12 @@ sub ModSerialStatus { # check if an alert must be sent... (= a letter is defined & status became "arrived" if ( $val->{letter} && $status == 2 && $oldstatus != 2 ) { require C4::Letters; - C4::Letters::SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} ); + eval { + C4::Letters::SendAlerts( 'issue', $val->{subscriptionid}, $val->{letter} ); + }; + if ( $@ ) { + #FIXME Do something... + } } } return; diff --git a/acqui/lateorders.pl b/acqui/lateorders.pl index 7c2b3f3..f33fa3c 100755 --- a/acqui/lateorders.pl +++ b/acqui/lateorders.pl @@ -111,7 +111,7 @@ if ($op and $op eq "send_alert"){ }; if ( $@ ) { $template->param(error_claim => $@); - } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_email" ) { + } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_to_address" ) { $template->{VARS}->{'error_claim'} = "no_email"; } else { $template->{VARS}->{'info_claim'} = 1; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt index 8cce08d..9393273 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/claims.tt @@ -157,8 +157,10 @@

Claims

[% IF error_claim %] - [% IF ( error_claim == "no_email" ) %] -
This vendor has no email
+ [% IF ( error_claim == "no_vendor_email" ) %] +
This vendor has no email.
+ [% ELSIF ( error_claim == "no_loggedin_user_email" ) %] +
No email is configured for this account.
[% ELSE %]
[% error_claim %]
[% END %] diff --git a/members/memberentry.pl b/members/memberentry.pl index b7123f2..9e7953e 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -367,7 +367,7 @@ if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){ }; if ( $@ ) { $template->param(error_alert => $@); - } elsif ( ref($err) eq "HASH" && defined $err->{error} and $err->{error} eq "no_email" ) { + } elsif ( ref($err) eq "HASH" && defined $err->{error} and $err->{error} eq "no_to_address" ) { $template->{VARS}->{'error_alert'} = "no_email"; } else { $template->{VARS}->{'info_alert'} = 1; diff --git a/serials/claims.pl b/serials/claims.pl index 446143e..24487ee 100755 --- a/serials/claims.pl +++ b/serials/claims.pl @@ -70,8 +70,12 @@ if (@serialnums) { # i.e. they have been flagged to generate claims }; if ( $@ ) { $template->param(error_claim => $@); - } elsif ( ref $err and exists $err->{error} and $err->{error} eq "no_email" ) { - $template->{VARS}->{'error_claim'} = "no_email"; + } elsif ( ref $err and exists $err->{error} ) { + if ( $err->{error} eq "no_to_address" ) { + $template->{VARS}->{'error_claim'} = "no_vendor_email"; + } elsif ( $err->{error} =~ m|Bad or missing From address| ) { + $template->{VARS}->{'error_claim'} = "no_loggedin_user_email"; + } } else { $template->{VARS}->{'info_claim'} = 1; } -- 1.7.10.4