Advanced Procmail Rules

Procmail supports nested rules—for example, to sort mail depending on the subject and to add a header:

:0
* ^Subject:\/.*
{
    :0
    * MATCH ?? [IMPORTANT]
    {
        :0 fwh
        | formail -I "X-Type: IMPORTANT"
        :0:
        Folder1/Important
    }
    :0
    * $MATCH ?? [VERY IMPORTANT]
    {
        :0 fwh
        | formail -I "X-Type: URGENT"
        :0:
        Folder1/Urgent
        :0c
        ! urgent@domain.net
    }
}

Tip

Nonstandard headers are required to start with X-.

The text matching after the special expression \/ is copied to the variable, which can then be used within the same rule.

In this rule, if the subject starts with [IMPORTANT], a new header is added and the mail is moved to the directory Folder1/Important. If the subject starts with [VERY IMPORTANT], a new header is added and the mail is moved to the directory Folder1/Urgent, and then sent to the recipe and to urgent@domain.net.

A weight can be assigned to each condition. For a condition to match, its weight is added to a score. If the final score is 1 or more, there is a match and the action is executed. Scoring is often used to identify spam:

:0:
* −2^0
* 1^0 ^Subject:.*win
* 2^0 ^Subject:.*viagara
* 1^0 ^Subject:.*\<cheap\>
Spam.mail

Tip

\< and \> delimits a word. The character before and after cheap cannot be in the character class [^a-zA-Z0-9_]. So, \<cheap\> is equivalent to [^a-zA-Z0-9_]cheap[^a-zA-Z0-9_]. This ensures that the condition does not match on cheaper, for example.

In this example, the initial score is set to −2. If this line is not present, the initial score is 0. If the subject contains "win", one point is added. If the subject contains "Viagra", two points are added to the score, etc. If the final score is 1 or more, the mail is moved to the file Spam.mail. This rule matches whether the subject contains at least "Viagra" and "win" or "Viagra" and "cheap".