Mail Management With Procmail - I, Robot (
Page 6 of 8 )
You've already seen how procmail can be
set up to forward incoming email to another email address, or to append it to a
mailbox. You might also remember that there's a third option - pipe the mail to
an external program for further processing. More often that not, this program is
"formail", a companion program that ships with the procmail
distribution.
Formail is a mail formatter, used primarily to extract or
manipulate the headers of an email message. Its applications include creating
new email messages on the fly, checking the value of a header in a received
message, rewriting exiting headers with new data, removing unwanted headers,
forcing messages into a standard mailbox format and splitting up a message
digest or mailbox into individual messages.
Formail makes it possible to
do some very creative things with your email - and one of its most common
applications includes creating a simple auto-responder for your email when
you're away from your computer. Consider the following recipe, which
demonstrates:
:0
| (/usr/bin/formail -r ; cat autoresponse.txt) | /usr/sbin/sendmail -oi
| -t
In this case, every message received is piped through formail, which
uses the contents of the file "autoresponse.txt" to generate a reply back to the
original sender.
It should be noted that the recipe above is purely
illustrative and should not be used in a live environment, as it does not
include error handling for email loops or messages from mailing lists; refer to
the procmail manual for a more comprehensive example.
You can also use
procmail in combination with the SpamAssassin spam filter (
http://www.spamassassin.org/) to
automatically detect and block spam. All you need to do is pipe each message
through SpamAssassin, and let it check to see if the message matches any of its
spam rulesets. Based on the results of its heuristic tests, SpamAssassin
automatically tags each message with an "X-Spam-Status" header indicating
whether or not it is spam; this header can then be used by procmail to filter
spam out of your regular mail spool into your "SPAM" mailbox, or send it
straight to the trash can.
Here's how this might work.
:0fw:
| /usr/local/bin/spamassassin
:0:
* ^X-Spam-Status: Yes
SPAM
More information on how SpamAssassin can be used with procmail is
available at
http://www.spamassassin.org/