I have a cron job that if it fails outputs HTML. If it succeeds it outputs nothing, in which case I get no mail. If it does fail and write HTML, cron sends me the mail but it comes as plain text and is hard to read.
I came up with two partial solutions but have not figured out how to make it work really well. The first is to make my job print MIME specifiers at the top of the output, like this:
Mime-Version: 1.0 Content-Type: text/html <html>...
Then I run it like this in cron:
my-job | sendmail my.name
The main problem with this is I get mail even if there is no text at all. A secondary problem is that I had to add the MIME type lines to my script, which is sort of the wrong place for it (not elegant, but maybe tolerable).
Then I tried using
mailx, which have a
-E option to skip sending if there is no text:
my-job | mail -E my.name
That does pretty much what cron does by default. Yet I can't figure out any way to set the MIME type with mail/mailx! Some references online say it is done using the
-a option, but on my system (Fedora 16),
mail -a takes an attachment filename, not a header line like
Content-Type: text/html as it apparently does on some other systems.
I don't want to make a temporary file, check its size, etc. I think I want one of these:
- A way to make cron mail readable as HTML.
- To make sendmail skip empty messages.
- To set a MIME type in mailx.
- Some other approach using a different program which is installed by default on Fedora.