From b497ef2c7041a6c71ab22b885b2212c7aca7dccf Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 6 Aug 2020 15:07:10 -0300 Subject: [PATCH] Bug 22343: Adapt controller scripts This patch adapts controller scripts that used sendmail. --- basket/sendbasket.pl | 51 +++++++++++++++++++-------------- misc/cronjobs/runreport.pl | 10 +++---- opac/opac-sendbasket.pl | 57 ++++++++++++++++++++++--------------- opac/opac-sendshelf.pl | 43 ++++++++++++++++------------ virtualshelves/sendshelf.pl | 46 +++++++++++++++++------------- 5 files changed, 119 insertions(+), 88 deletions(-) diff --git a/basket/sendbasket.pl b/basket/sendbasket.pl index 2076c425fa..f3db60d4bd 100755 --- a/basket/sendbasket.pl +++ b/basket/sendbasket.pl @@ -20,7 +20,7 @@ use Modern::Perl; use CGI qw ( -utf8 ); use Encode qw(encode); use Carp; -use Mail::Sendmail; +use Email::Sender::Simple qw(sendmail); use MIME::QuotedPrint; use MIME::Base64; @@ -30,6 +30,7 @@ use C4::Auth; use C4::Output; use C4::Templates (); use Koha::Email; +use Koha::SMTP::Servers; use Koha::Token; my $query = new CGI; @@ -44,10 +45,10 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( } ); -my $bib_list = $query->param('bib_list') || ''; -my $email_add = $query->param('email_add'); +my $bib_list = $query->param('bib_list') || ''; +my $email_add = $query->param('email_add'); -my $dbh = C4::Context->dbh; +my $dbh = C4::Context->dbh; if ( $email_add ) { output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' ) @@ -55,9 +56,8 @@ if ( $email_add ) { session_id => scalar $query->cookie('CGISESSID'), token => scalar $query->param('csrf_token'), }); - my $email = Koha::Email->new(); - my %mail = $email->create_message_headers({ to => $email_add }); - my $comment = $query->param('comment'); + my $email = Koha::Email->create({ to => $email_add }); + my $comment = $query->param('comment'); # Since we are already logged in, no need to check credentials again # when loading a second template. @@ -109,13 +109,17 @@ if ( $email_add ) { my $template_res = $template2->output(); my $body; + my $subject; # Analysing information and getting mail properties - if ( $template_res =~ /(.*)/s ) { - $mail{subject} = $1; - $mail{subject} =~ s|\n?(.*)\n?|$1|; - $mail{subject} = encode('MIME-Header',$mail{subject}); + if ( $template_res =~ /(?.*)/s ) { + $subject = $+{subject}; + $subject =~ s|\n?(.*)\n?|$1|; + } + else { + $subject = "no subject"; } - else { $mail{'subject'} = "no subject"; } + + $email->header_set( 'Subject', encode('MIME-Header', $subject) ); my $email_header = ""; if ( $template_res =~ /
(.*)/s ) { @@ -139,10 +143,10 @@ if ( $email_add ) { my $boundary = "====" . time() . "===="; # Writing mail - $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; + $email->header_set( 'content-type', "multipart/mixed; boundary=\"$boundary\"" ); my $isofile = encode_base64(encode("UTF-8", $iso2709)); $boundary = '--' . $boundary; - $mail{body} = <param( SENT => "1" ); + $email->body_set( $THE_body ); + + try { + my $library = Koha::Patrons->find( $borrowernumber )->library; + my $smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); + Email::Sender::Simple::sendmail( $email, { transport => $smtp_server->transport } ); + $template->param( SENT => "1" ); } - else { - # do something if it doesn't work.... - carp "Error sending mail: $Mail::Sendmail::error \n"; + catch { + carp "Error sending mail: $_"; $template->param( error => 1 ); - } + }; + $template->param( email_add => $email_add ); output_html_with_http_headers $query, $cookie, $template->output; } diff --git a/misc/cronjobs/runreport.pl b/misc/cronjobs/runreport.pl index 6732afae9c..1f50483190 100755 --- a/misc/cronjobs/runreport.pl +++ b/misc/cronjobs/runreport.pl @@ -318,12 +318,12 @@ foreach my $report_id (@ARGV) { $message = "$message"; $args->{contenttype} = 'text/html'; } - my $email = Koha::Email->new(); - my %mail = $email->create_message_headers($args); - $mail{Data} = $message; - $mail{Auth} = { user => $username, pass => $password, method => $method } if $username; + my $email = Koha::Email->create( $args ); + my %headers = $email->header_pairs; + $headers{Data} = $message; + $headers{Auth} = { user => $username, pass => $password, method => $method } if $username; - my $msg = MIME::Lite->new(%mail); + my $msg = MIME::Lite->new(%headers); $msg->attach( Type => "text/$format", diff --git a/opac/opac-sendbasket.pl b/opac/opac-sendbasket.pl index d5e6c7fdfd..d0a748182e 100755 --- a/opac/opac-sendbasket.pl +++ b/opac/opac-sendbasket.pl @@ -22,7 +22,7 @@ use Modern::Perl; use CGI qw ( -utf8 ); use Encode qw(encode); use Carp; -use Mail::Sendmail; +use Email::Sender::Simple qw(sendmail); use MIME::QuotedPrint; use MIME::Base64; @@ -34,6 +34,7 @@ use C4::Members; use C4::Templates (); use Koha::Email; use Koha::Patrons; +use Koha::SMTP::Servers; use Koha::Token; my $query = new CGI; @@ -57,7 +58,6 @@ if ( $email_add ) { session_id => scalar $query->cookie('CGISESSID'), token => scalar $query->param('csrf_token'), }); - my $email = Koha::Email->new(); my $patron = Koha::Patrons->find( $borrowernumber ); my $borcat = $patron ? $patron->categorycode : q{}; my $user_email = $patron->first_valid_email_address @@ -67,11 +67,12 @@ if ( $email_add ) { my $comment = $query->param('comment'); # if you want to use the KohaAdmin address as from, that is the default no need to set it - my %mail = $email->create_message_headers({ - to => $email_add, + my $email = Koha::Email->create({ + to => $email_add, replyto => $email_replyto, }); - $mail{'X-Abuse-Report'} = C4::Context->preference('KohaAdminEmailAddress'); + + $email->header_set( 'X-Abuse-Report', C4::Context->preference('KohaAdminEmailAddress') ); # Since we are already logged in, no need to check credentials again # when loading a second template. @@ -129,13 +130,16 @@ if ( $email_add ) { my $body; # Analysing information and getting mail properties - - if ( $template_res =~ /(.*)/s ) { - $mail{subject} = $1; - $mail{subject} =~ s|\n?(.*)\n?|$1|; - $mail{subject} = encode('MIME-Header',$mail{subject}); + my $subject; + if ( $template_res =~ /\(?.*)\/s ) { + $subject = $+{subject}; + $subject =~ s|\n?(.*)\n?|$1|; + } + else { + $subject = "no subject"; } - else { $mail{'subject'} = "no subject"; } + + $email->header_set( 'Subject', encode('MIME-Header', $subject) ); my $email_header = ""; if ( $template_res =~ /
(.*)/s ) { @@ -156,14 +160,13 @@ if ( $email_add ) { $body = encode_qp(Encode::encode("UTF-8", $body)); } - $mail{body} = $body; - my $boundary = "====" . time() . "===="; - $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; + $email->header_set( 'content-type', "multipart/mixed; boundary=\"$boundary\"" ); + my $isofile = encode_base64(encode("UTF-8", $iso2709)); $boundary = '--' . $boundary; - $mail{body} = <param( SENT => "1" ); + $email->body_set( $THE_body ); + + if ( !defined $iso2709 ) { + carp "Error sending mail: empty basket"; + $template->param( error => 1 ); } else { - # do something if it doesn't work.... - carp "Error sending mail: empty basket" if !defined($iso2709); - carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error; - $template->param( error => 1 ); + try { + my $library = $patron->library; + my $smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); + Email::Sender::Simple::sendmail( $email, { transport => $smtp_server->transport } ); + $template->param( SENT => "1" ); + } + catch { + carp "Error sending mail: $_"; + $template->param( error => 1 ); + }; } + $template->param( email_add => $email_add ); output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 }; } diff --git a/opac/opac-sendshelf.pl b/opac/opac-sendshelf.pl index acbc27e6d2..a5ff6324de 100755 --- a/opac/opac-sendshelf.pl +++ b/opac/opac-sendshelf.pl @@ -23,7 +23,7 @@ use CGI qw ( -utf8 ); use Encode qw( encode ); use Carp; -use Mail::Sendmail; +use Email::Sender::Simple qw(sendmail); use MIME::QuotedPrint; use MIME::Base64; use C4::Auth; @@ -33,6 +33,7 @@ use C4::Output; use C4::Members; use Koha::Email; use Koha::Patrons; +use Koha::SMTP::Servers; use Koha::Virtualshelves; my $query = new CGI; @@ -61,10 +62,9 @@ my $shelf = Koha::Virtualshelves->find( $shelfid ); if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { if ( $email ) { - my $message = Koha::Email->new(); my $comment = $query->param('comment'); - my %mail = $message->create_message_headers( + my $email = Koha::Email->create( { to => $email, } @@ -128,13 +128,17 @@ if ( $email ) { my $template_res = $template2->output(); my $body; + my $subject; # Analysing information and getting mail properties - if ( $template_res =~ /(.*)/s ) { - $mail{subject} = $1; - $mail{subject} =~ s|\n?(.*)\n?|$1|; + if ( $template_res =~ /(?.*)/s ) { + $subject = $+{subject}; + $subject =~ s|\n?(.*)\n?|$1|; } - else { $mail{'subject'} = "no subject"; } - $mail{subject} = encode('MIME-Header', $mail{subject}); + else { + $subject = "no subject"; + } + + $email->header_set( 'Subject', encode('MIME-Header', $subject) ); my $email_header = ""; if ( $template_res =~ /
(.*)/s ) { @@ -158,12 +162,12 @@ if ( $email ) { my $boundary = "====" . time() . "===="; # We set and put the multipart content - $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; + $email->header_set( 'content-type', "multipart/mixed; boundary=\"$boundary\"" ); my $isofile = encode_base64(encode("UTF-8", $iso2709)); $boundary = '--' . $boundary; - $mail{body} = <param( SENT => "1" ); + $email->body_set( $THE_body ); + + try { + my $library = Koha::Patrons->find( $borrowernumber )->library; + my $smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); + Email::Sender::Simple::sendmail( $email, { transport => $smtp_server->transport } ); + $template->param( SENT => "1" ); } - else { - # do something if it doesn't work.... - carp "Error sending mail: $Mail::Sendmail::error \n"; + catch { + carp "Error sending mail: $_"; $template->param( error => 1 ); - } + }; $template->param( shelfid => $shelfid, - email => $email, + email => $email, ); output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 }; diff --git a/virtualshelves/sendshelf.pl b/virtualshelves/sendshelf.pl index 0103df3e71..55084ccd0a 100755 --- a/virtualshelves/sendshelf.pl +++ b/virtualshelves/sendshelf.pl @@ -23,7 +23,7 @@ use CGI qw ( -utf8 ); use Encode qw( encode ); use Carp; -use Mail::Sendmail; +use Email::Sender::Simple qw(sendmail); use MIME::QuotedPrint; use MIME::Base64; use C4::Auth; @@ -31,6 +31,7 @@ use C4::Biblio; use C4::Items; use C4::Output; use Koha::Email; +use Koha::SMTP::Servers; use Koha::Virtualshelves; my $query = new CGI; @@ -45,17 +46,16 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( } ); -my $shelfid = $query->param('shelfid'); -my $email = $query->param('email'); +my $shelfid = $query->param('shelfid'); +my $to_address = $query->param('email'); my $dbh = C4::Context->dbh; -if ($email) { +if ($to_address) { my $comment = $query->param('comment'); - my $message = Koha::Email->new(); - my %mail = $message->create_message_headers( + my $email = Koha::Email->create( { - to => $email + to => $to_address } ); @@ -108,13 +108,17 @@ if ($email) { my $template_res = $template2->output(); my $body; + my $subject; # Analysing information and getting mail properties - if ( $template_res =~ /(.*)/s ) { - $mail{subject} = $1; - $mail{subject} =~ s|\n?(.*)\n?|$1|; + if ( $template_res =~ /(?.*)/s ) { + $subject = $+{subject}; + $subject =~ s|\n?(.*)\n?|$1|; } - else { $mail{'subject'} = "no subject"; } - $mail{subject} = encode( 'MIME-Header', $mail{subject} ); + else { + $subject = "no subject"; + } + + $email->header_set( 'Subject', encode('MIME-Header', $subject) ); my $email_header = ""; if ( $template_res =~ /
(.*)/s ) { @@ -138,12 +142,12 @@ if ($email) { my $boundary = "====" . time() . "===="; # We set and put the multipart content - $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; + $email->header_set( 'content-type', "multipart/mixed; boundary=\"$boundary\"" ); my $isofile = encode_base64( encode( "UTF-8", $iso2709 ) ); $boundary = '--' . $boundary; - $mail{body} = <body_set( $THE_body ); - # do something if it works.... + try { + my $library = Koha::Patrons->find( $borrowernumber )->library; + my $smtp_server = Koha::SMTP::Servers->get_effective_server({ library => $library }); + Email::Sender::Simple::sendmail( $email, { transport => $smtp_server->transport } ); $template->param( SENT => "1" ); } - else { - # do something if it doesn't work.... - carp "Error sending mail: $Mail::Sendmail::error \n"; + catch { + carp "Error sending mail: $_"; $template->param( error => 1 ); - } + }; $template->param( email => $email ); output_html_with_http_headers $query, $cookie, $template->output; -- 2.28.0