From b5391c84044f7de5b6b1a0ddf3acb7aa60bf64da Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 18 Nov 2021 09:40:22 -0300
Subject: [PATCH] Bug 29501: Make gather_print_notices.pl use Koha::Email

This patch makes the cronjob script use Koha::Email and thus relying on
configured SMTP settings instead of just trying localhost.
---
 misc/cronjobs/gather_print_notices.pl | 29 ++++++++++++++-------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/misc/cronjobs/gather_print_notices.pl b/misc/cronjobs/gather_print_notices.pl
index a777acbbcf..1f70d6a726 100755
--- a/misc/cronjobs/gather_print_notices.pl
+++ b/misc/cronjobs/gather_print_notices.pl
@@ -20,8 +20,8 @@ use Getopt::Long qw( GetOptions );
 use C4::Log qw( cronlogaction );
 
 use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Email;
 use Koha::Util::OpenDocument qw( generate_ods );
-use MIME::Lite;
 
 my (
     $help,
@@ -151,7 +151,7 @@ sub print_notices {
     my $format = $params->{format} // 'html';
 
     die "Format $format is not known"
-        unless $format =~ m[^html$|^csv$|^ods$];
+        unless $format =~ m/^html$|^csv$|^ods$/;
 
     my ( @filenames, $messages_by_branch );
 
@@ -287,11 +287,12 @@ sub send_files {
     my $from = $params->{from};
     return unless $to and $from;
 
-    my $mail = MIME::Lite->new(
-        From     => $from,
-        To       => $to,
-        Subject  => 'Print notices for ' . $today_syspref,
-        Type     => 'multipart/mixed',
+    my $email = Koha::Email->create(
+        {
+            from    => $from,
+            to      => $to,
+            subject => 'Print notices for ' . $today_syspref,
+        }
     );
 
     while ( my ( $type, $filenames ) = each %$files ) {
@@ -306,20 +307,20 @@ sub send_files {
 
             next unless $mimetype;
 
-            my $filepath = File::Spec->catdir( $directory, $filename );
+            my $filepath = File::Spec->catfile( $directory, $filename );
 
             next unless $filepath or -f $filepath;
 
-            $mail->attach(
-              Type     => $mimetype,
-              Path     => $filepath,
-              Filename => $filename,
-              Encoding => 'base64',
+            $email->attach_file(
+                $filepath,
+                content_type => $mimetype,
+                name         => $filename,
+                disposition  => 'attachment',
             );
         }
     }
 
-    $mail->send;
+    $email->send_or_die;
 }
 
 =head1 NAME
-- 
2.32.0