From fa72cea8b636607ce8ec811ee9202c3bcec51070 Mon Sep 17 00:00:00 2001 From: David Bourgault Date: Fri, 15 Sep 2017 15:06:13 -0400 Subject: [PATCH] Bug 8000 - Redirect all emails to SendAllEmailsTo Koha/Email.pm Test plan: 1) Apply path 2) Clear all SendAllEmailsTo system preference 3) Send mail to a patron of your choosing, email will go to patron's email address as usual. 4) Set SendAllEmailsTo to a test email address 5) Send mail to the patron, email will be redirected to the email set in the systempreference. It does not affect messages in the message_queue. This patch obsoletes previous patches, because it achieves the same functionality in a much more centralized way. (4 lines of code!) --- Koha/Email.pm | 6 ++++++ .../mysql/atomicupdate/Bug8000-SendAllEmailsTo.syspref.sql | 2 ++ installer/data/mysql/sysprefs.sql | 3 ++- .../prog/en/modules/admin/preferences/admin.pref | 5 +++++ t/db_dependent/Letters.t | 12 +++++++++++- 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/Bug8000-SendAllEmailsTo.syspref.sql diff --git a/Koha/Email.pm b/Koha/Email.pm index fc392a3..9f63e39 100644 --- a/Koha/Email.pm +++ b/Koha/Email.pm @@ -18,6 +18,7 @@ package Koha::Email; use Modern::Perl; +use Email::Valid; use base qw(Class::Accessor); use C4::Context; @@ -49,6 +50,11 @@ sub create_message_headers { From => $params->{from}, charset => $params->{charset} ); + + if (C4::Context->preference('SendAllEmailsTo') && Email::Valid->address(C4::Context->preference('SendAllEmailsTo'))) { + $mail{'To'} = C4::Context->preference('SendAllEmailsTo'); + } + if ( C4::Context->preference('ReplytoDefault') ) { $params->{replyto} ||= C4::Context->preference('ReplytoDefault'); } diff --git a/installer/data/mysql/atomicupdate/Bug8000-SendAllEmailsTo.syspref.sql b/installer/data/mysql/atomicupdate/Bug8000-SendAllEmailsTo.syspref.sql new file mode 100644 index 0000000..37bdb9c --- /dev/null +++ b/installer/data/mysql/atomicupdate/Bug8000-SendAllEmailsTo.syspref.sql @@ -0,0 +1,2 @@ +INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) +VALUES ('SendAllEmailsTo','',NULL,'All emails will be redirected to this email if it is not empty','free'); \ No newline at end of file diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 23cefb8..22903b5 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -1,4 +1,4 @@ -INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES +INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type('SendAllEmailsTo','',NULL,'All emails will be redirected to this email if it is not empty','free'),` ) VALUES ('AcqCreateItem','ordering','ordering|receiving|cataloguing','Define when the item is created : when ordering, when receiving, or in cataloguing module','Choice'), ('AcqEnableFiles','0',NULL,'If enabled, allows librarians to upload and attach arbitrary files to invoice records.','YesNo'), ('AcqItemSetSubfieldsWhenReceiptIsCancelled','', '','Upon cancelling a receipt, update the items subfields if they were created when placing an order (e.g. o=5|a="bar foo")', 'Free'), @@ -483,6 +483,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('SelfCheckoutByLogin','1',NULL,'Have patrons login into the web-based self checkout system with their username/password or their cardnumber','YesNo'), ('SelfCheckReceiptPrompt','1','NULL','If ON, print receipt dialog pops up when self checkout is finished','YesNo'), ('SelfCheckTimeout','120','','Define the number of seconds before the Web-based Self Checkout times out a patron','Integer'), +('SendAllEmailsTo','',NULL,'All emails will be redirected to this email if it is not empty','free'), ('SeparateHoldings','0',NULL,'Separate current branch holdings from other holdings','YesNo'), ('SeparateHoldingsBranch','homebranch','homebranch|holdingbranch','Branch used to separate holdings','Choice'), ('SessionRestrictionByIP','1','Check for change in remote IP address for session security. Disable only when remote IP address changes frequently.','','YesNo'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref index 2e8dce7..aa0a211 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/admin.pref @@ -16,6 +16,11 @@ Administration: class: email - "If you leave this empty, the From address will be used (often defaulting to the admin address)." - + - "Email to redirect all messages to: " + - pref: SendAllEmailsTo + class: email + - "(Leave this field empty to send messages to their normal recipient)" + - - "How much debugging information to show in the browser when an internal error occurs: " - pref: DebugLevel default: 0 diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index e20cefb..c70d25b 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 77; +use Test::More tests => 79; use Test::MockModule; use Test::Warn; @@ -514,6 +514,7 @@ my $borrowernumber = AddMember( ); my $alert_id = C4::Letters::addalert($borrowernumber, 'issue', $subscriptionid); +t::lib::Mocks::mock_preference( 'SendAllEmailsTo', '' ); my $err2; warning_is { @@ -523,6 +524,15 @@ $err2 = SendAlerts( 'issue', $serial->{serialid}, 'RLIST' ) } is($err2, 1, "Successfully sent serial notification"); is($mail{'To'}, 'john.smith@test.de', "mailto correct in sent serial notification"); is($mail{'Message'}, 'Silence in the library,'.$subscriptionid.',No. 0', 'Serial notification text constructed successfully'); + +t::lib::Mocks::mock_preference( 'SendAllEmailsTo', 'robert.tables@mail.com' ); + +my $err3; +warning_is { +$err3 = SendAlerts( 'issue', $serial->{serialid}, 'RLIST' ) } + "Fake sendmail", + "SendAlerts is using the mocked sendmail routine"; +is($mail{'To'}, 'robert.tables@mail.com', "mailto address overwritten by SendAllMailsTo preference"); } subtest 'GetPreparedLetter' => sub { -- 2.7.4