Bug 11678: Gather print notices: add --email parameter
authorJonathan Druart <jonathan.druart@biblibre.com>
Thu, 23 Jan 2014 12:12:30 +0000 (13:12 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Wed, 4 Nov 2015 14:10:19 +0000 (11:10 -0300)
This patch add the ability to send generated files by email.

You can specify several emails.

Test plan:
- same as Bug 11603
- call the script misc/cronjobs/gather_print_notices.pl with the
  following parameters:

perl misc/cronjobs/gather_print_notices.pl /tmp/test --csv --ods --html
--letter_code=CHECKIN -d=: --email="email_one@example.org"
--email="email_two@example.org"

Signed-off-by: Frederic Demians <f.demians@tamil.fr>
  - Works as desribed. Rebased upon bug-11603.

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
misc/cronjobs/gather_print_notices.pl

index d573f04..a511326 100755 (executable)
@@ -22,6 +22,7 @@ use C4::Log;
 
 use File::Basename qw( dirname );
 use Koha::DateUtils;
+use MIME::Lite;
 
 my (
     $stylesheet,
@@ -33,6 +34,7 @@ my (
     $delimiter,
     @letter_codes,
     $send,
+    @emails,
 );
 
 $send = 1;
@@ -45,6 +47,7 @@ GetOptions(
     'd|delimiter:s' => \$delimiter,
     'letter_code:s' => \@letter_codes,
     'send!'         => \$send,
+    'e|email:s'     => \@emails,
 ) || pod2usage(1);
 
 pod2usage(0) if $help;
@@ -123,6 +126,22 @@ if ( $html ) {
     });
 }
 
+if ( @emails ) {
+    my $files = {
+        html => $html_filenames,
+        csv  => $csv_filenames,
+        ods  => $ods_filenames,
+    };
+    for my $email ( @emails ) {
+        send_files({
+            directory => $output_directory,
+            files => $files,
+            to => $email,
+            from => C4::Context->preference('KohaAdminEmailAddress'), # Should be replaced if bug 8000 is pushed
+        });
+    }
+}
+
 sub print_notices {
     my ( $params ) = @_;
 
@@ -280,13 +299,56 @@ sub generate_ods {
     $doc->save();
 }
 
+sub send_files {
+    my ( $params ) = @_;
+    my $directory = $params->{directory};
+    my $files = $params->{files};
+    my $to = $params->{to};
+    my $from = $params->{from};
+    return unless $to and $from;
+
+    my $mail = MIME::Lite->new(
+        From     => $from,
+        To       => $to,
+        Subject  => 'Print notices for ' . $today->output(),
+        Type     => 'multipart/mixed',
+    );
+
+    while ( my ( $type, $filenames ) = each %$files ) {
+        for my $filename ( @$filenames ) {
+            my $mimetype = $type eq 'html'
+                ? 'text/html'
+                : $type eq 'csv'
+                    ? 'text/csv'
+                    : $type eq 'ods'
+                        ? 'application/vnd.oasis.opendocument.spreadsheet'
+                        : undef;
+
+            next unless $mimetype;
+
+            my $filepath = File::Spec->catdir( $directory, $filename );
+
+            next unless $filepath or -f $filepath;
+
+            $mail->attach(
+              Type     => $mimetype,
+              Path     => $filepath,
+              Filename => $filename,
+              Encoding => 'base64',
+            );
+        }
+    }
+
+    $mail->send;
+}
+
 =head1 NAME
 
 gather_print_notices - Print waiting print notices
 
 =head1 SYNOPSIS
 
-gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-h|--help]
+gather_print_notices output_directory [-s|--split] [--html] [--csv] [--ods] [--letter_code=LETTER_CODE] [-e|--email=your_email@example.org] [-h|--help]
 
 Will print all waiting print notices to the output_directory.
 
@@ -337,6 +399,10 @@ This is the same as the csv parameter but using csv2odf to generate an ods file
 Filter print messages by letter_code.
 Several letter_code parameters can be given.
 
+=item B<-e|--email>
+
+E-mail address to send generated files.
+
 =item B<-h|--help>
 
 Print a brief help message