A sample header looks like this:
From: Cron Daemon <email@example.com> To: firstname.lastname@example.org Subject: Cron <root@hostname> /usr/local/bin/foobar --whateverOf the above, only the To: address makes any sense. The first thing I tried to change was the subject line. This is fairly easy using the
10 15 * * * /usr/local/bin/foobar --whatever | mail -s "Pretty subject" email@example.comThe mail now looks like this:
From: Cron Daemon <firstname.lastname@example.org> To: email@example.com Subject: Pretty subjectThis is a big step forward, because we can now filter emails based on subject. It still doesn't let us change the from address though. That's when I remembered using
sendmaila long time ago to send mail through perl scripts, so I had a look through the sendmail man page, and found a few interesting flags:
-F full_name Set the sender full name. This is used only with messages that have no From: message header. -f sender Set the envelope sender address. This is the address where delivery problems are sent to, unless the message contains an Errors-To: message header.This lets you set the From header, but there's no way to change the Subject. Since we can't use
sendmailtogether, we can either set the From address or the Subject, but not both. I then remembered the
-tflag to sendmail that tells it to read the header from the message itself. The only thing left to do was to add the header into the output of every command. Since I couldn't easily do this in cron, I wrote my own script to do it. It's called
pmail. The p is for pretty.
( echo -e "From: $from_name <$from>\r\nSubject: $subject\r\nTo: $to\r\n\r\n"; cat ) | /usr/sbin/sendmail -tIt writes the From and Subject headers, then leaves a blank line, and then passes its STDIN directly on to sendmail. The script has 3 optional parameters — the sender's address, sender's name and subject, and one mandatory parameter — the recipient's address(es).
To use it, set your cron line something like this:
* * * * * /usr/local/bin/foobar --whatever | \ pmail -F "Philip Tellis" -f "firstname.lastname@example.org" -s "Pretty mail from foobar" email@example.comNote: lines wrapped for readability.
You can also add the current date to the subject line like this:
... -s "Foobar mail on $( date +\%Y-\%m-\%d )" ...Remember that you have to escape
%signs in crontab lines otherwise cron will translate them to newline characters.
Umm, you also need to have sendmail set up correctly, and doing that is your problem :)
It's not complete yet, but it works for my use case. If you like it, use it. It's under the BSD license. I'll probably add a Reply-To option if it makes sense for me, and at some point add in data validation too, but not today.