Howto filter and forward e-mail with procmail: example
Procmail is a program that can process e-mails according to a set of rules defined in a configuration file. Rules consist of condition/action pairs. The condition can be any test related to the email header or content. The action can be forwarding the e-mail to a program or placing it into a mailbox. This howto uses a simple example to explain how to dispatch e-mails to different mailboxes according to pre-defined filters.
Invoking the procmail mechanism
In typical uses, procmail is instantiated by the local MTA through the .forward script. The .forward scrip placed in the user's home directory would contain the following lines:
| /usr/bin/procmail
With this setting, all e-mails will be forwarded to the procmail program. From there, procmail will be responsible for delivering them to a local mailbox, forwarding to an e-mail address or delivering them to another program.
Configuring procmail
To tell procmail what to do with e-mails that it receives from the local MTA, we need to create a configuration file.
Typically, each account using the .forward script will also need to create a .procmailrc configuration file. Below is an example from a configuration file that I am using. Amongst other things it causes procmail to deliver e-mails from mailing lists to appropriate mailboxes.
# Change these according to your environment MAILDIR=$HOME/Mail DEFAULT=$MAILDIR/mbox LOGFILE=$HOME/from LOCKFILE=$HOME/.lockmail BACKUP=other@email.com # Save e-mails from somewhere.com mailing list to the SOMEWHERE mailbox :0: * ^From.*@somewhere\.com SOMEWHERE # Save e-mails from IETF mailing lists to the IETF mailbox :0: * List-Id:.*ietf IETF # Save Important e-mails to the IMPORTANT mailbox, then forward a copy to the back-up e-mail. :0: * ^Subject.*Important* { :0 c IMPORTANT :0 ! $BACKUP } # Place the e-mail into a folder by date (e.g. Dec-2009). :0: * `date +%b-%Y`
| Labels: howto, unix |
|

Comment