+
+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_syspref,
+ 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] [-e|--email=your_email@example.org] [-h|--help]
+
+Will print all waiting print notices to the output_directory.
+
+The generated filename will be notices-TODAY.[csv|html|ods] or notices-TODAY-BRANCHCODE.[csv|html|ods] if the --split parameter is given.
+
+=head1 OPTIONS
+
+=over
+
+=item B<output_directory>
+
+Define the output directory where the files will be generated.
+
+=item B<--send|--nosend>
+
+After files have been generated, messages status is changed from 'pending' to
+'sent'. This is the default action, without this parameter or with --send.
+Using --nosend, the message status is not changed.
+
+=item B<-s|--split>
+
+Split messages into separate files by borrower home library to OUTPUT_DIRECTORY/notices-CURRENT_DATE-BRANCHCODE.[csv|html|ods]
+
+=item B<--html>
+
+Generate the print notices in a html file (default is --html, if --csv and --ods are not given).
+
+=item B<--csv>
+
+Generate the print notices in a csv file.
+If you use this parameter, the template should contain 2 lines.
+The first one the csv headers and the second one the value list.
+
+For example:
+cardnumber:patron:email:item
+<<borrowers.cardnumber>>:<<borrowers.firstname>> <<borrowers.surname>>:<<borrowers.email>>:<<items.barcode>>
+
+You have to combine this option with one (and only one) letter_code.
+
+=item B<--ods>
+
+Generate the print notices in a ods file.
+
+This is the same as the csv parameter but using csv2odf to generate an ods file instead of a csv file.
+
+=item B<--letter_code>
+
+Filter print messages by letter_code.
+Several letter_code parameters can be given.
+
+=item B<-e|--email>
+
+Repeatable.
+E-mail address to send generated files to.
+
+=item B<-h|--help>
+
+Print a brief help message
+
+=back
+
+=head1 AUTHOR
+
+Jesse Weaver <pianohacker@gmail.com>
+
+Jonathan Druart <jonathan.druart@biblibre.com>
+
+=head1 COPYRIGHT
+
+Copyright 2009 Jesse Weaver
+
+Copyright 2014 BibLibre
+
+=head1 LICENSE
+This file is part of Koha.
+
+Koha is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+Koha is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+=cut