Can cron e-mail HTML?

Derek Ragona derek at
Sun Jul 15 15:57:58 UTC 2007

At 05:02 PM 7/14/2007, Mikhail Teterin wrote:
>Derek Ragona wrote:
>= = I'd rather avoid poluting my script with e-mail sending code...
>= You need to change your script to send the email itself.
>Thank you, Derek, but -- as I stated already -- I wanted to see, if this can
>be avoided...

Doing so makes it very non-portable.  This would make your cron changes 
needed on any system and version you want to implement this behavior.

>Since you posted your script, I'll comment on it. First of all, you don't 
>ksh for anything you are doing in this script. FreeBSD's /bin/sh is enough
>(we aren't Solaris :-) -- but is 9 times smaller here (amd64).

I don't need ksh for the particular sample I gave you, which is pared down 
from any I use.  I posted a simple boilerplate that anyone who reads the 
list can use.  For most things /bin/sh works well, but the overhead of ksh 
on a modern server is negligible.

>Now, instead of redirecting each line of output into MAILFILE, then mailing,
>and removing it, you should be either outputing everything directly into
>         {
>                 cat $REPORT_LOG_HEADER
>                 echo " " >> $MAILFILE
>                 echo " " >> $MAILFILE
>                 echo "<BR> <BR>" >> $MAILFILE
>                 ....
>                 cat $REPORT_LOG_FOOTER
>         } | $MAIL -s "the report name" $MAILTO

This was done as an example.  In many cases I have reports generated in 
html, and simple email the URL of the report.

>or, if you want to use the temporary file, use exec to redirect into it
>_once_, instead of _on every line_:
>         exec > $MAILFILE
>         cat $REPORT_LOG_HEADER
>         printf " \n \n<BR><BR>\n"
>         $MAIL -s "the report name" $MAILTO < $MAILFILE
>         $RM $MAILFILE
>This may look nicer, but is not, because temporary files are nasty, and you
>need to be sure, that you remove them in case you are interrupted (trapping
>signals, etc.)

I removed the signal handling from my example as I didn't want to add that 
much complexity.  It is trivial to add proper signal handling.

>I was surprised, one can not redirect into a pipe directly. The following did
>not work, as I expected, with neither /bin/sh nor /usr/local/bin/ksh93. The
>following, I thought, would be the same as my first example, only
>         exec | $MAIL -s "the report name" $MAILTO
>         cat .....
>But it is not. So you have to chose from one of the first two examples.

This is UNIX.  Meaning there are many ways to accomplish tasks.  Just 
choose the tools and methods you want.  I strive to make cron scripts 
simple and portable.  I support a multitude of servers running different 
UNIX versions and flavors.


More information about the freebsd-questions mailing list