Bug 12544 - Send scheduled reports as an attachment

This patch adds the ability to add the report as an attached file
to the sent email.

Test Plan:
1) Email yourself a test report
2) Apply this patch
3) Repeat step 1, note there is no difference
4) Add the -a parameter, note your also recieve the report as an attachment

Sponsored-by: Briar Cliff University

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Julius Fleschner <Julius.fleschner@briarcliff.edu>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
This commit is contained in:
Kyle Hall 2015-11-03 14:43:17 -05:00 committed by Brendan A Gallagher
parent b6880abc50
commit 265761c353

View file

@ -23,12 +23,13 @@ use warnings;
use C4::Reports::Guided; # 0.12 use C4::Reports::Guided; # 0.12
use C4::Context; use C4::Context;
use Koha::Email;
use C4::Log; use C4::Log;
use Koha::Email;
use Koha::DateUtils;
use Getopt::Long qw(:config auto_help auto_version); use Getopt::Long qw(:config auto_help auto_version);
use Pod::Usage; use Pod::Usage;
use Mail::Sendmail; use MIME::Lite;
use Text::CSV_XS; use Text::CSV_XS;
use CGI qw ( -utf8 ); use CGI qw ( -utf8 );
use Carp; use Carp;
@ -60,6 +61,7 @@ runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ]
--format=s selects format. Choice of text, html, csv, or tsv --format=s selects format. Choice of text, html, csv, or tsv
-e --email whether to use e-mail (implied by --to or --from) -e --email whether to use e-mail (implied by --to or --from)
-a --attachment additionally attach the report as a file. cannot be used with html format
--username username to pass to the SMTP server for authentication --username username to pass to the SMTP server for authentication
--password password to pass to the SMTP server for authentication --password password to pass to the SMTP server for authentication
--method method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc. --method method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc.
@ -161,6 +163,7 @@ my $help = 0;
my $man = 0; my $man = 0;
my $verbose = 0; my $verbose = 0;
my $email = 0; my $email = 0;
my $attachment = 0;
my $format = "text"; my $format = "text";
my $to = ""; my $to = "";
my $from = ""; my $from = "";
@ -181,6 +184,7 @@ GetOptions(
'from=s' => \$from, 'from=s' => \$from,
'subject=s' => \$subject, 'subject=s' => \$subject,
'email' => \$email, 'email' => \$email,
'a|attachment' => \$attachment,
'username:s' => \$username, 'username:s' => \$username,
'password:s' => \$password, 'password:s' => \$password,
'method:s' => \$method, 'method:s' => \$method,
@ -214,6 +218,8 @@ unless (scalar(@ARGV)) {
} }
($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n"; ($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
my $today = dt_from_string();
my $date = $today->ymd();
foreach my $report_id (@ARGV) { foreach my $report_id (@ARGV) {
my $report = get_saved_report($report_id); my $report = get_saved_report($report_id);
@ -272,22 +278,31 @@ foreach my $report_id (@ARGV) {
$message .= $csv->string() . "\n"; $message .= $csv->string() . "\n";
} }
} }
if ($email){
if ($email) {
my $args = { to => $to, from => $from, subject => $subject }; my $args = { to => $to, from => $from, subject => $subject };
if ($format eq 'html') { if ( $format eq 'html' ) {
$message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>"; $message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
$args->{contenttype} = 'text/html'; $args->{contenttype} = 'text/html';
} }
$args->{message} = $message;
my $email = Koha::Email->new(); my $email = Koha::Email->new();
my %mail = $email->create_message_headers($args); my %mail = $email->create_message_headers($args);
$mail{'Auth'} = {user => $username, pass => $password, method => $method} if $username; $mail{Data} = $message;
sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error; $mail{Auth} = { user => $username, pass => $password, method => $method } if $username;
} else {
my $msg = MIME::Lite->new(%mail);
$msg->attach(
Type => "text/$format",
Data => encode( 'utf8', $message ),
Filename => "report$report_id-$date.$format",
Disposition => 'attachment',
) if $attachment;
$msg->send();
carp "Mail not sent" unless $msg->last_send_successful();
}
else {
print $message; print $message;
} }
# my @xmlarray = ... ;
# my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
# my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
# store_results($id,$xml);
} }