Results 1 to 3 of 3
  1. #1

    Post Procmail 101: Intro to Server-Side Email Filtering

    Procmail is an incredibly powerful mail processing application already running on your WestHost VPS. By adding custom recipes to either the global /etc/procmailrc file (applies to all incoming emails) or the user-level .promailrc file in the user's home directory (usually located at /ftp/pub/<username>/.procmailrc), you can quickly create server-side email filters. Keep in mind that with POP mail, there are no folders and only your inbox is stored on the server. While you can still setup procmail recipes to automatically delete certain messages or deliver certain messages to other POP inboxes, no amount of procmail magic can do server-side sorting of messages into folders with POP mail. In other words, if you really want to take full advantage of all that procmail has to offer, now would be a good time to switch your WestHost mail to the IMAP protocol.

    First, a quick primer on procmail. All procmail recipes have the same basic syntax shown below. The first line of every recipe begins with :0 to indicate that a new recipe is beginning, and the last line provides the action to be performed on the matching messages. Any lines in the middle specify conditions that the message must match in order to have the action applied.

    :0 [flags] [:]
    * zero or more conditions
    Let's look at an example recipe to get started:

    # Delete spam with a score of 8 or higher
    * ^X-Spam-Level: \*\*\*\*\*\*\*\*
    The first line, beginning with #, is a comment that will be ignored by procmail, allowing us to document our recipes so we remember what they are trying to accomplish when we look at the procmailrc file at some point in the future. The next line begins the recipe. This recipe has one condition: It looks in the X-Spam-Level header for at least 10 asterisks in a row (indicating a score from SpamAssassin of at least 10 points). The action for this recipe is to move matching messages to the special /dev/null file, causing them to be deleted. In other words, if a message matches the condition (i.e., if the message is almost certainly spam), then it is deleted. Since this recipe works by looking at the headers added to the message by SpamAssassin, you must both 1) have SpamAssassin installed, and (if you are adding this to the global file) 2) place this recipe somewhere below the the #---/SPAMASSASSIN--- section that the Site Manager has automatically added.

    Let's look at one more spam-related recipe before we move on. The following recipe will move all spam messages to the user's IMAP Spam folder. Like the previous recipe, this will only work if SpamAssassin is installed and (if added to the global procmail file) it appear below the #---/SPAMASSASSIN--- section that the Site Manager has automatically added. Note the ending : on the first line of the recipe. This cause procmail to use a lockfile, preventing corrupted mail files, duplicates, and lost messages. In general, you should always use an ending : if your recipe doesn't delete messages. When working with the Dovecot IMAP server, remember to end your destination folder with a trailing slash (note the period before the folder name and the trailing slash after .Spam/).
    # Move spam to the appropriate user's Spam folder (requires Dovecot IMAP)
    * ^X-Spam-Status: Yes
    Keep in mind that only one recipe is applied to each message, so the order of procmail recipes is very important. Procmail attempts to match incoming messages against each recipe, one at a time and in the order they appear in the global file (after this, and if nothing in the global file matched, the same happens for the recipes in the user-level .procmailrc file, if it exists). As soon as one of the recipes matches, that action is applied to the message and procmail exits, not processing any more recipes for that message. If we want a message to continue to be processed after one of the recipes is applied, we need to specify the continue flag in the first line of the recipe.

    Let's look at a sample recipe that uses the continue flag:

    # Send a text message for legitimate emails
    :0 c:
    * ! ^FROM_DAEMON
    * ! ^X-Spam-Status: Yes
    The c in the first line of the recipe is the continue flag, telling procmail that it should keep processing the message as usual after applying this recipe. Since we are not deleting the message, we also need the : at the end of the first line. This time we have two conditions; the message must match both in order for the action to be applied. ^FROM_DAEMON is a special condition that matches messages sent by the mailer daemon/mail delivery subsystem. In other words, it matches bounceback errors and warning. Our second condition is more familiar, matching messages marked as spam by SpamAssassin. The ! operator negates the two conditions; in other words, these conditions match messages that are not bounceback errors and are not spam. In the action line, the ! causes mail to be forwarded to the specified email address. While something similar could be achieved using an email alias or the AutoForwarder feature in Site Manager, the procmail recipe allows you to specify which messages get sent as a text message. Also unlike the Site Manager, you can easily apply the forwarding to ALL incoming emails by placing it into the global file instead of the user-level .procmailrc file.


    Update: Below are a few more procmail recipes.
    # Delete spam (according to the user/global SpamAssassin preferences)
    * ^X-Spam-Status: Yes
    # Delete ALL incoming mail sent to the user advertisingresponder@
    # This is useful for "do not reply" addresses that nobody checks
    * LOGNAME ?? ^^advertisingresponder^^
    Last edited by nventurella; 12-16-2009 at 05:59 PM. Reason: Added recipes

  2. #2



    Great post. I just wanted to add a link here for anyone who might be interested in learning about some more advanced procmail recipes:

    Mark Cox
    Data Center Administrator

    When you expect more from your Web host

  3. #3
    Moderator wildjokerdesign's Avatar
    Join Date
    Jun 2003
    Kansas City Mo


    That really breaks things down very well. Helps make sense of what is going on! Thanks so much for taking the time to post this Nick.
    Please remember your charity of choice:

    Handy Links: | Plain Text Editors: EditPlus | Crimson

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts