Procmail recipes

07 Mar, 2005

I recently installed Nullmailer on my router and fileserver for them to send any mail on to my desktop machine. By doing so, it's much easier to keep track of any output from cron, logcheck or similar crucial e-mail messages.

This worked fine, but I needed some way to mark email by the sender hostname. I found a good procmail recipe from Timo's procmail tips and recipes:

# mail sent to root
# put the hostname in the subject header
# courtesy of http://www.uwasa.fi/~ts/info/proctips.html
:0
* ^TO.*root
{
  :0 fwh
  | formail -I"Subject: [${HOST_}] ${SUBJ_}"
  :0
  $MAILDIR/root/
}

And this part:

# get the subject discarding any leading and trailing blanks
# note: On some systems -xSubject: has to be -x"Subject: "
SUBJ_=`formail -xSubject: | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`

I also had to write a regexp to get the \$HOST from the Message Id in the mail:

# get the hostname from the message id
HOST_=`formail -xMessage-Id: | sed -e 's/\( < .*@\)//' -e 's/>//'`

There are most likely easier ways of doing this, but it fills the purpose nicely. My complete hacked up procmail is available in my dotfiles dir.