From 52f004144d638225526734989eea742f984fb1f0 Mon Sep 17 00:00:00 2001
From: Jason Stephenson <jason@sigio.com>
Date: Fri, 8 Feb 2019 15:47:41 -0500
Subject: [PATCH] Lp 1801163: Switch to Email::MIME in SendEmail A/T Reactor

Switch from Email::Simple to Email::MIME Perl module in the SendEmail
Action/Trigger Reactor.  Email::MIME properly encodes unescaped header
fields when added to the message with the header_str_set method.

We allow only 1 of each address field to be created while doing the
encoding, so that messages conform to RFC 2822.

This commit adds a new prerequisite as mentioned in the release notes,
so be sure to install the prerequisites for your Linux distribution
before installing.

You can test this with concerto data from a fresh installation by:

1. Configuring your test system to send email.

2. Updating all actor.usr entries to have your email address.

3. Updating the New User Created Welcome Notice event definiton to
   active = true.

4. Run the action_trigger_runner with --process-hooks --run-pending.

5. You should get 237 new user welcome emails.  The exact number is
   subject to change.

Signed-off-by: Jason Stephenson <jason@sigio.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Jason Boyer <JBoyer@eoli.info>
---
 Open-ILS/src/extras/install/Makefile.debian-jessie    |  1 +
 Open-ILS/src/extras/install/Makefile.debian-stretch   |  1 +
 Open-ILS/src/extras/install/Makefile.ubuntu-bionic    |  1 +
 Open-ILS/src/extras/install/Makefile.ubuntu-xenial    |  1 +
 .../OpenILS/Application/Trigger/Reactor/SendEmail.pm  | 19 +++++++++++++------
 .../Administration/sendemail-reactor-mime.adoc        |  8 ++++++++
 6 files changed, 25 insertions(+), 6 deletions(-)
 create mode 100644 docs/RELEASE_NOTES_NEXT/Administration/sendemail-reactor-mime.adoc

diff --git a/Open-ILS/src/extras/install/Makefile.debian-jessie b/Open-ILS/src/extras/install/Makefile.debian-jessie
index 21da86c75c..d30fa4dd25 100644
--- a/Open-ILS/src/extras/install/Makefile.debian-jessie
+++ b/Open-ILS/src/extras/install/Makefile.debian-jessie
@@ -38,6 +38,7 @@ export DEBS = \
 	libdbi-dev\
 	libdbi1\
 	libemail-simple-perl\
+	libemail-mime-perl\
 	libexcel-writer-xlsx-perl\
 	libgd-graph3d-perl\
 	liblocale-maketext-lexicon-perl\
diff --git a/Open-ILS/src/extras/install/Makefile.debian-stretch b/Open-ILS/src/extras/install/Makefile.debian-stretch
index e1491d1522..d16e1a51bf 100644
--- a/Open-ILS/src/extras/install/Makefile.debian-stretch
+++ b/Open-ILS/src/extras/install/Makefile.debian-stretch
@@ -38,6 +38,7 @@ export DEBS = \
 	libdbi-dev\
 	libdbi1\
 	libemail-simple-perl\
+	libemail-mime-perl\
 	libexcel-writer-xlsx-perl\
 	libgd-graph3d-perl\
 	liblocale-maketext-lexicon-perl\
diff --git a/Open-ILS/src/extras/install/Makefile.ubuntu-bionic b/Open-ILS/src/extras/install/Makefile.ubuntu-bionic
index 35c42fde10..f1478a308c 100644
--- a/Open-ILS/src/extras/install/Makefile.ubuntu-bionic
+++ b/Open-ILS/src/extras/install/Makefile.ubuntu-bionic
@@ -37,6 +37,7 @@ export DEBS = \
 	libdbi-dev\
 	libdbi1\
 	libemail-simple-perl\
+	libemail-mime-perl\
 	libexcel-writer-xlsx-perl\
 	libgd-graph3d-perl\
 	liblocale-maketext-lexicon-perl\
diff --git a/Open-ILS/src/extras/install/Makefile.ubuntu-xenial b/Open-ILS/src/extras/install/Makefile.ubuntu-xenial
index 8802f2478f..e8251b7cfd 100644
--- a/Open-ILS/src/extras/install/Makefile.ubuntu-xenial
+++ b/Open-ILS/src/extras/install/Makefile.ubuntu-xenial
@@ -38,6 +38,7 @@ export DEBS = \
 	libdbi-dev\
 	libdbi1\
 	libemail-simple-perl\
+	libemail-mime-perl\
 	libexcel-writer-xlsx-perl\
 	libgd-graph3d-perl\
 	liblocale-maketext-lexicon-perl\
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm
index 9dd76ee454..675ab69cde 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm
@@ -3,7 +3,7 @@ use strict; use warnings;
 use Error qw/:try/;
 use Data::Dumper;
 use Email::Send;
-use Email::Simple;
+use Email::MIME;
 use OpenSRF::Utils::SettingsClient;
 use OpenILS::Application::Trigger::Reactor;
 use OpenSRF::Utils::Logger qw/:logger/;
@@ -29,7 +29,7 @@ Email is encoded in UTF-8 and the corresponding MIME-Version, Content-Type,
 and Content-Transfer-Encoding headers are set to help mail user agents
 decode the content.
 
-The From, To, Subject, Bcc, Cc, Reply-To and Sender -fields are
+The From, To, Bcc, Cc, Reply-To, Sender, and Subject fields are
 automatically MIME-encoded.
 
 No default template is assumed, and all information other than the
@@ -56,13 +56,20 @@ sub handler {
     my $stat;
     my $err;
 
-    my $email = Email::Simple->new($text);
+    my $email = Email::MIME->new($text);
 
-    for my $hfield (qw/From To Subject Bcc Cc Reply-To Sender/) {
-	my @headers = $email->header($hfield);
-	$email->header_set($hfield => map { encode("MIME-Header", $_) } @headers) if ($headers[0]);
+    # Handle the address fields.  In addition to encoding the values
+    # properly, we make sure there is only 1 each.
+    for my $hfield (qw/From To Bcc Cc Reply-To Sender/) {
+        my @headers = $email->header($hfield);
+        $email->header_str_set($hfield => join(',', @headers)) if ($headers[0]);
     }
 
+    # Handle the Subject field.  Again, the standard says there can be
+    # only one.
+    my @headers = $email->header('Subject');
+    $email->header_str_set('Subject' => $headers[0]) if ($headers[0]);
+
     $email->header_set('MIME-Version' => '1.0');
     $email->header_set('Content-Type' => "text/plain; charset=UTF-8");
     $email->header_set('Content-Transfer-Encoding' => '8bit');
diff --git a/docs/RELEASE_NOTES_NEXT/Administration/sendemail-reactor-mime.adoc b/docs/RELEASE_NOTES_NEXT/Administration/sendemail-reactor-mime.adoc
new file mode 100644
index 0000000000..b6cb8d5eb5
--- /dev/null
+++ b/docs/RELEASE_NOTES_NEXT/Administration/sendemail-reactor-mime.adoc
@@ -0,0 +1,8 @@
+SendEmail Reactor Updated to use Email::MIME
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+The SendEmail reactor for Action/Trigger has been updated to use the
+Email::MIME Perl module for proper encoding of the email message
+header fields.  You should notice no functional difference in the
+sending of emails.  This change does add a new prerequisite package,
+so be sure to run the prerequisite installation procedure for your
+Linux distribution before upgrading Evergreen.
-- 
2.11.0