Bug 10777: email HTML reports with the proper Content-Type
authorNicholas van Oudtshoorn <vanoudt@gmail.com>
Thu, 22 Aug 2013 08:56:25 +0000 (16:56 +0800)
committerGalen Charlton <gmc@esilibrary.com>
Tue, 11 Mar 2014 14:57:23 +0000 (14:57 +0000)
The misc/cronjobs/runreport.pl allows for sending html reports
via email. The problem is that the Content-Type isn't set to
text/html, which means that the generated html email isn't
displayed properly.

This patch set the Content-Type, and also adds a tiny bit of
CSS to potentially alternate row colours (just to make long
reports a bit easier on the eye!)

TEST PLAN
----------
1. Run the script similar to this:
    ./misc/cronjobs/runreport.pl --format=html --to=YOUREMAIL --subject="Bad Formatting!" REPORTNUMBER
2. Look at the email - the html code should by visible and ugly.
3. apply the patch
4. Run the script again.
5. Look at the email - the data should look nicer now.

Signed-off-by: Mark Tompsett <mtompset@hotmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
misc/cronjobs/runreport.pl

index 2357ed8..18e175d 100755 (executable)
@@ -241,14 +241,25 @@ foreach my $report_id (@ARGV) {
             $message .= $csv->string() . "\n";
         }
     }
-
     if ($email){
-        my %mail = (
-            To      => $to,
-            From    => $from,
-            Subject => encode('utf8', $subject ),
-            Message => encode('utf8', $message )
-        );
+        my %mail;
+        if ($format eq 'html') {
+                $message = "<html><head><style>tr:nth-child(n+1) { background-color: #ccc;}</style></head><body>$message</body></html>";
+           %mail = (
+              To      => $to,
+              From    => $from,
+              'Content-Type' => 'text/html',
+              Subject => encode('utf8', $subject ),
+              Message => encode('utf8', $message )
+          );
+        } else {
+          %mail = (
+              To      => $to,
+              From    => $from,
+              Subject => encode('utf8', $subject ),
+              Message => encode('utf8', $message )
+          );
+        }
         sendmail(%mail) or carp 'mail not sent:' . $Mail::Sendmail::error;
     } else {
         print $message;