View | Details | Raw Unified | Return to bug 22343
Collapse All | Expand All

(-)a/misc/cronjobs/runreport.pl (-23 / +42 lines)
Lines 27-41 use C4::Context; Link Here
27
use C4::Log;
27
use C4::Log;
28
use Koha::Email;
28
use Koha::Email;
29
use Koha::DateUtils;
29
use Koha::DateUtils;
30
use Koha::SMTP::Servers;
30
31
31
use Getopt::Long qw(:config auto_help auto_version);
32
use Getopt::Long qw(:config auto_help auto_version);
32
use Pod::Usage;
33
use Pod::Usage;
33
use MIME::Lite;
34
use Text::CSV::Encoded;
34
use Text::CSV::Encoded;
35
use CGI qw ( -utf8 );
35
use CGI qw ( -utf8 );
36
use Carp;
36
use Carp;
37
use Encode;
37
use Encode;
38
use JSON qw( to_json );
38
use JSON qw( to_json );
39
use Try::Tiny;
39
40
40
BEGIN {
41
BEGIN {
41
    # find Koha's Perl modules
42
    # find Koha's Perl modules
Lines 176-182 Reports - Guided Reports Link Here
176
my $help    = 0;
177
my $help    = 0;
177
my $man     = 0;
178
my $man     = 0;
178
my $verbose = 0;
179
my $verbose = 0;
179
my $email   = 0;
180
my $send_email = 0;
180
my $attachment = 0;
181
my $attachment = 0;
181
my $format  = "text";
182
my $format  = "text";
182
my $to      = "";
183
my $to      = "";
Lines 201-207 GetOptions( Link Here
201
    'from=s'            => \$from,
202
    'from=s'            => \$from,
202
    'subject=s'         => \$subject,
203
    'subject=s'         => \$subject,
203
    'param=s'           => \@params,
204
    'param=s'           => \@params,
204
    'email'             => \$email,
205
    'email'             => \$send_email,
205
    'a|attachment'      => \$attachment,
206
    'a|attachment'      => \$attachment,
206
    'username:s'        => \$username,
207
    'username:s'        => \$username,
207
    'password:s'        => \$password,
208
    'password:s'        => \$password,
Lines 226-233 if ($format eq 'tsv' || $format eq 'text') { Link Here
226
    $separator = "\t";
227
    $separator = "\t";
227
}
228
}
228
229
229
if ($to or $from or $email) {
230
if ($to or $from or $send_email) {
230
    $email = 1;
231
    $send_email = 1;
231
    $from or $from = C4::Context->preference('KohaAdminEmailAddress');
232
    $from or $from = C4::Context->preference('KohaAdminEmailAddress');
232
    $to   or $to   = C4::Context->preference('KohaAdminEmailAddress');
233
    $to   or $to   = C4::Context->preference('KohaAdminEmailAddress');
233
}
234
}
Lines 312-339 foreach my $report_id (@ARGV) { Link Here
312
        my $json = to_json( \@rows_to_store );
313
        my $json = to_json( \@rows_to_store );
313
        C4::Reports::Guided::store_results( $report_id, $json );
314
        C4::Reports::Guided::store_results( $report_id, $json );
314
    }
315
    }
315
    if ($email) {
316
    if ($send_email) {
316
        my $args = { to => $to, from => $from, subject => $subject };
317
318
        my $email = Koha::Email->new(
319
            {
320
                to      => $to,
321
                from    => $from,
322
                subject => $subject,
323
            }
324
        );
325
317
        if ( $format eq 'html' ) {
326
        if ( $format eq 'html' ) {
318
            $message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
327
            $message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
319
            $args->{contenttype} = 'text/html';
328
            $email->html_body($message);
329
        }
330
        else {
331
            $email->text_body($message);
320
        }
332
        }
321
        my $email = Koha::Email->create( $args );
333
322
        my %headers = $email->header_pairs;
334
        $email->attach(
323
        $headers{Data} = $message;
335
            encode( 'utf8', $message ),
324
        $headers{Auth} = { user => $username, pass => $password, method => $method } if $username;
336
            content_type => "text/$format",
325
337
            name         => "report$report_id-$date.$format",
326
        my $msg = MIME::Lite->new(%headers);
338
            disposition  => 'attachment',
327
328
        $msg->attach(
329
            Type        => "text/$format",
330
            Data        => encode( 'utf8', $message ),
331
            Filename    => "report$report_id-$date.$format",
332
            Disposition => 'attachment',
333
        ) if $attachment;
339
        ) if $attachment;
334
340
335
        $msg->send();
341
        my $smtp_server = Koha::SMTP::Servers->get_default;
336
        carp "Mail not sent" unless $msg->last_send_successful();
342
        $smtp_server->set(
343
            {
344
                user_name => $username,
345
                password  => $password,
346
            }
347
        )
348
            if $username;
349
350
        $email->transport( $smtp_server->transport );
351
        try {
352
            $email->send_or_die;
353
        }
354
        catch {
355
            carp "Mail not sent: $_";
356
        };
337
    }
357
    }
338
    else {
358
    else {
339
        print $message;
359
        print $message;
340
- 

Return to bug 22343