Procmail is a mail delivery agent (MDA) or mail filter. Among many other things, it can be used for advanced filtering tasks that web hosts’ GUI tools cannot accommodate.
In this example we will use procmail to perform several common tasks:
- Delete obvious spam
- Place probable spam in a special spam folder, not in the inbox
- Place mail with **SPAM** in the subject in the spam folder
- Delete mail from a particular sender
- Place everything else in the inbox
- Log everything for testing purposes
This example presumes that SpamAssassin is running on the server and has already scored incoming mail before procmail receives it. A common newbie error is to try to use procmail to directly detect spam, often by looking for specific words in the subject or body. Far better to let SpamAssassin, a spam filter, do the detection and have procmail, a delivery filter, handle the delivery. If you don’t already have SpamAssassin running on your mail server, go do that first and come back here later.
I use IMAP hosted with Pair Networks, but the discussion that follows should be broadly applicable to any remote or local mail server. For purposes of illustration, let’s assume we have the domain “example.com”, our hosting account’s username is “account”, and the mailbox we will be working with is “firstname.lastname@example.org”. Pair Networks uses mbox format, but as far as I know the following will work equally well with maildir format.
Consider the following example procmail script:
# Comment out next 2 lines to turn off logging LOGFILE=/usr/home/account/procmail_log VERBOSE=on # Make this script, not qmail, deliver all mail EXITCODE=99 # General format for filters: # - Lock (:0:) or not (:0) the file to be written to as needed # - Filter on header # - Move to indicated folder # Delete mail with spam score of 9 or greater :0 * ^X-Spam-Level: ********* /dev/null # Put remaining mail flagged as spam in spam box :0: * ^X-Spam-Flag:.*YES /usr/boxes/account/example.com/user^/.imap/INBOX.Spam # Put mail with **SPAM** in subject in spam box # Catches mail flagged as spam by another system and then forwarded to this # box, which did not flag it. Caution: Generates false positives :0: * ^Subject:.***SPAM** /usr/boxes/account/example.com/user^/.imap/INBOX.Spam # Delete mail from a particular sender :0 * ^From:.*email@example.com /dev/null # Any other items go here, before the last item # Last item: put everything else in normal mailbox :0: /usr/boxes/account/example.com/user
Season to taste. Name it (typically .procmailrc) and upload the script to the server at /usr/home/account.
Now enable the script. Pair Networks customers should enter the Account Control Center (ACC) and open Email Management. Find the appropriate domain and click on “This domain has X recipes and Y mailboxes.” Find the user’s mailbox and click on the “[add recipe]” link to the right of it. Insure that you have a single email address selected, firstname.lastname@example.org, and select the “Filter” option. Press “Proceed”.
In the “E-Mail Filter” field, enter (changing the path as needed):
procmail -f- /usr/home/account/.procmailrc
Enable Junk E-Mail Filtering and press “Create Filter”. Note that it may take up to 10 minutes to become active.
Send yourself test messages to insure that mail is being properly filtered. Note that filtered mail may take a few minutes to arrive. Use the logfile to debug. Once all is well, comment out logging from .procmailrc and delete the logfile.
TODO: Investigate if it is possible to use a site-wide .procmailrc file to perform site-wide mail filtering tasks instead of having to have a separate .procmailrc for every mailbox.
ALTERNATIVES TO PROCMAIL
Procmail is complicated and unmaintained; maildrop is often recommended as a simpler modern replacement.