From 3dc7096ca93094ef877b8d60447dfcf837f74b07 Mon Sep 17 00:00:00 2001
From: Ulrich Kleiber <ulrich.kleiber@bsz-bw.de>
Date: Wed, 5 Feb 2020 11:04:20 +0100
Subject: [PATCH] Bug 24588: Prevent e-mails from Koha to be flagged as spam

Set the e-mail headers Mailer-X and MessageID by Koha,
to gain a better positive score from the spam filter.

To test:
1) Let Koha send an e-mail message.
2) Check the header of this e-mail message with your e-mail client.
   There should be no X-Mailer header with value 'Koha'. If there
   is an MessageID header, it is probably set by the MSA, e.g. Exim.
   Example:
      Message-Id: <E1iyZRY-0006XT-0t@FQDN>
3) Apply patch.
4) Repeat step 1.
5) Check the header of the new e-mail message with your e-mail client.
   There should be a X-Mailer header with value 'Koha' and a MessageID
   header with a value set by Koha.
   Example:
      X-Mailer: Koha
      Message-Id: <15807387030.B731e.18490@FQDN>
6) Sign off.
---
 C4/Installer/PerlDependencies.pm | 5 +++++
 Koha/Email.pm                    | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm
index f949bd790c..22afe471a3 100644
--- a/C4/Installer/PerlDependencies.pm
+++ b/C4/Installer/PerlDependencies.pm
@@ -104,6 +104,11 @@ our $PERL_DEPS = {
         'required' => '1',
         'min_ver'  => '1.103'
     },
+    'Email::MessageID' => {
+        'usage'    => 'Core',
+        'required' => '1',
+        'min_ver'  => '1.406'
+    },
     'Exception::Class' => {
         'usage'    => 'Core',
         'required' => '1.38',
diff --git a/Koha/Email.pm b/Koha/Email.pm
index 58340756e8..d0c2adea74 100644
--- a/Koha/Email.pm
+++ b/Koha/Email.pm
@@ -19,6 +19,7 @@ package Koha::Email;
 
 use Modern::Perl;
 use Email::Valid;
+use Email::MessageID;
 
 use base qw(Class::Accessor);
 use C4::Context;
@@ -70,6 +71,8 @@ sub create_message_headers {
     $mail{'Message'}      = $params->{message}     if $params->{message};
     $mail{'Subject'}      = $params->{subject}     if $params->{subject};
     $mail{'Content-Type'} = $params->{contenttype} if $params->{contenttype};
+    $mail{'X-Mailer'}     = "Koha";
+    $mail{'Message-ID'}   = Email::MessageID->new->in_brackets;
     return %mail;
 }
 1;
-- 
2.11.0