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

(-)a/misc/cronjobs/runreport.pl (-29 / +46 lines)
Lines 23-34 use warnings; Link Here
23
23
24
use C4::Reports::Guided; # 0.12
24
use C4::Reports::Guided; # 0.12
25
use C4::Context;
25
use C4::Context;
26
use Koha::Email;
27
use C4::Log;
26
use C4::Log;
27
use Koha::Email;
28
use Koha::DateUtils;
28
29
29
use Getopt::Long qw(:config auto_help auto_version);
30
use Getopt::Long qw(:config auto_help auto_version);
30
use Pod::Usage;
31
use Pod::Usage;
31
use Mail::Sendmail;
32
use MIME::Lite;
32
use Text::CSV_XS;
33
use Text::CSV_XS;
33
use CGI qw ( -utf8 );
34
use CGI qw ( -utf8 );
34
use Carp;
35
use Carp;
Lines 60-65 runreport.pl [ -h | -m ] [ -v ] reportID [ reportID ... ] Link Here
60
   --format=s      selects format. Choice of text, html, csv, or tsv
61
   --format=s      selects format. Choice of text, html, csv, or tsv
61
62
62
   -e --email      whether to use e-mail (implied by --to or --from)
63
   -e --email      whether to use e-mail (implied by --to or --from)
64
   -a --attachment additionally attach the report as a file. cannot be used with html format
63
   --username      username to pass to the SMTP server for authentication
65
   --username      username to pass to the SMTP server for authentication
64
   --password      password to pass to the SMTP server for authentication
66
   --password      password to pass to the SMTP server for authentication
65
   --method        method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc.
67
   --method        method is the type of authentication. Ie. LOGIN, DIGEST-MD5, etc.
Lines 161-166 my $help = 0; Link Here
161
my $man     = 0;
163
my $man     = 0;
162
my $verbose = 0;
164
my $verbose = 0;
163
my $email   = 0;
165
my $email   = 0;
166
my $attachment = 0;
164
my $format  = "text";
167
my $format  = "text";
165
my $to      = "";
168
my $to      = "";
166
my $from    = "";
169
my $from    = "";
Lines 181-186 GetOptions( Link Here
181
    'from=s'            => \$from,
184
    'from=s'            => \$from,
182
    'subject=s'         => \$subject,
185
    'subject=s'         => \$subject,
183
    'email'             => \$email,
186
    'email'             => \$email,
187
    'a|attachment'      => \$attachment,
184
    'username:s'        => \$username,
188
    'username:s'        => \$username,
185
    'password:s'        => \$password,
189
    'password:s'        => \$password,
186
    'method:s'          => \$method,
190
    'method:s'          => \$method,
Lines 214-219 unless (scalar(@ARGV)) { Link Here
214
}
218
}
215
($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
219
($verbose) and print scalar(@ARGV), " argument(s) after options: " . join(" ", @ARGV) . "\n";
216
220
221
my $today = dt_from_string();
222
my $date = $today->ymd();
217
223
218
foreach my $report_id (@ARGV) {
224
foreach my $report_id (@ARGV) {
219
    my $report = get_saved_report($report_id);
225
    my $report = get_saved_report($report_id);
Lines 269-303 foreach my $report_id (@ARGV) { Link Here
269
            $message .= $csv->string() . "\n";
275
            $message .= $csv->string() . "\n";
270
        }
276
        }
271
    }
277
    }
272
    if ($email){
278
279
    if ($email) {
273
        my $email = Koha::Email->new();
280
        my $email = Koha::Email->new();
274
        my %mail;
281
        my %mail;
275
        if ($format eq 'html') {
282
        if ( $format eq 'html' ) {
276
                $message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
283
            $message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
277
           %mail = $email->create_message_headers({
284
            %mail = $email->create_message_headers(
278
              to      => $to,
285
                {
279
              from    => $from,
286
                    to          => $to,
280
              contenttype => 'text/html',
287
                    from        => $from,
281
              subject => encode('utf8', $subject ),
288
                    contenttype => 'text/html',
282
              message => encode('utf8', $message )
289
                    subject     => encode( 'utf8', $subject ),
283
           }
290
                }
284
          );
291
            );
285
        } else {
286
          %mail = $email->create_message_headers ({
287
              to      => $to,
288
              from    => $from,
289
              subject => encode('utf8', $subject ),
290
              message => encode('utf8', $message )
291
          }
292
          );
293
        }
292
        }
294
        $mail{'Auth'} = {user => $username, pass => $password, method => $method} if $username;
293
        else {
295
        sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error;
294
            %mail = $email->create_message_headers(
296
    } else {
295
                {
296
                    to      => $to,
297
                    from    => $from,
298
                    subject => encode( 'utf8', $subject ),
299
                }
300
            );
301
        }
302
303
        $mail{Data} = encode( 'utf8', $message );
304
        $mail{Auth} = { user => $username, pass => $password, method => $method } if $username;
305
306
        my $msg = MIME::Lite->new(%mail);
307
308
        $msg->attach(
309
            Type        => "text/$format",
310
            Data        => encode( 'utf8', $message ),
311
            Filename    => "report$report_id-$date.$format",
312
            Disposition => 'attachment',
313
        ) if $attachment;
314
315
        $msg->send();
316
        carp "Mail not sent" unless $msg->last_send_successful();
317
    }
318
    else {
297
        print $message;
319
        print $message;
298
    }
320
    }
299
    # my @xmlarray = ... ;
300
    # my $url = "/cgi-bin/koha/reports/guided_reports.pl?phase=retrieve%20results&id=$id";
301
    # my $xml = XML::Dumper->new()->pl2xml( \@xmlarray );
302
    # store_results($id,$xml);
303
}
321
}
304
- 

Return to bug 12544