+ if ( $store_results ) {
+ my $json = to_json( \@rows_to_store );
+ C4::Reports::Guided::store_results( $report_id, $json );
+ }
+ if ($email) {
+ my $args = { to => $to, from => $from, subject => $subject };
+ if ( $format eq 'html' ) {
+ $message = "<html><head><style>tr:nth-child(2n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
+ $args->{contenttype} = 'text/html';
+ }
+ my $email = Koha::Email->new();
+ my %mail = $email->create_message_headers($args);
+ $mail{Data} = $message;
+ $mail{Auth} = { user => $username, pass => $password, method => $method } if $username;
+
+ 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 {