Introduction
Procmail is really only useful to you in a few instances. The first is if you read your mail from a unix machine (either working at a sun or using telnet/ssh to log into math and use pine/elm/mutt). The second instance is if you read your mail remotely using the IMAP protocol (note: our web based email uses this). If you read your mail at home using Eudora/Netscape/Outlook, and you don't know if you are using IMAP or not, the odds are good that you are not and this tutorial won't do you much good.
To start out, you'll need to create a file called .procmailrc where your procmail information is configured. Using a text editor (vi, emacs, pico, etc), in your home directory edit a file called .procmailrc. Paste the following contents into the file.
PATH=/bin:/usr/bin:/usr/local/binMAILDIR=$HOME/mailPMDIR=$HOME/.procmailLOGFILE=$PMDIR/logINCLUDERC=$PMDIR/general.rc
The only thing you may need to change on this is the MAILDIR line. If you use pine or an IMAP client to read your mail, set this to "MAILDIR=$HOME/mail". If you use elm, mutt, or the Sun mail program, set it to "MAILDIR=$HOME/Mail".
Next you will need to create your procmail directory. In your home directory on a unix machine, type "mkdir .procmail" and hit return. This will create the directory where you will store your procmail rules and log file. You'll notice the file file we created above has an entry for INCLUDERC. This is a file where you will store procmail rules. You can have more than one of these, however in the example above we only have one called general.rc. In this file you can create as many rules for filtering mail as you like. Simply create/edit the file general.rc in your .procmail directory and add the rules.
Procmail uses unix regular expressions to filter mail. Basically, you create rules using regular expressions, and any mail that matches the rule is filtered. Regular Expressions can be a bit tricky, but most likely you won't need to really understand them unless you want to do more advanced filtering. First let's take a look at some basic Regular Expressions.
Using regular expressions
The simplest type of regular expression is a string. For example, the regular expression "grads" will match anything that has the word grads in it. To get more complicated expressions, we need to look at some of the special characters.
.osu
A "." will match anything. The expression .osu will then match anything followed by the string "osu". For example, this would match the string "username.171@osu.edu".
username*
A "*" matches the previous character any number of times. In this example, this would match the string "usernam" followed by 0 or more e's (usernam, username, usernamee, usernameee would all get matched).
username+ and username?
These two are similar to "*" except that they match the previous character a different number of times. "+" will match one or more times, so it will match username, usernamee, usernameee, etc, as long as there is at least one e. "?" matches either 0 or 1 of the previous characters, so this would match either usernam or username.
user(name)*
Parentheses can be used to group characters together. In this example, the * applies to the characters in the parentheses instead of just the last character. So in this example, it would match user, username, usernamename, etc. "+" and "?" can also be used in place of the *.
[br]ob
Brackets can be used to lump together a list of characters that will match. In this example, it will match either the string "bob" or "rob".
[^aeiou]+
When the brackets begin with a ^ character, it means match anything except the characters that are within the bracket. So in this case, it will match any string that does not contain a vowel (aeiou). Because of the + sign after the brackets, it will match only a string that does not contain one or more vowel.
username1|username2
A vertical bar acts as a logical "or". In this example, it will match anything that contains either "username1" or "username2".
^To:
When the ^ does not appear inside brackets, it means to match at the beginning. In this example, it will match anything that begins with "To:" (which will become relevant later).
edu$
A $ means to match at the end of the string. In this case, it will match anything that ends in "edu".
\[hello\]
:0:
* regular expression
mail folder
This is the basic structure for a rule. All rules should begin with ":0:". The next line begins with a "*" followed by a space and then your regular expression. The third line contains the mail folder you want the filtered mail to go to. You can have multiple regular expression lines if you like, they must all match for the mail to be filtered.
Filtering by Subject
:0:
* ^Subject:.*\[COLUG\]
colug
This is a rule I use to filter out mail from a mailing list. In this case, the mailing list has the string "[COLUG]" in ever subject. The rule here is to match the string "Subject:" followed by 0 or more of any characters followed by the string "[COLUG]". Note that we could get rid of the .* here, however then it would not match replies that have subjects such as "Re: [COLUG] blah blah". The third line tells procmail to store the mail in a mailbox called colug.
Filtering by To header
:0:
* ^To:.*cunningham\.171@osu\.edu
osu
This rule will filter any mail that was sent to cunningham.171@osu.edu, and store it in a mailbox called osu. Since my osu.edu mail is forwarded to my math account, I use this rule to filter all the osu.edu mail to a seperate mailbox. Note that you have to excape the . in the email address because it is a special character.
More complete To filtering
:0:
* ^TOcunningham\.171@osu\.edu
osu
This one is very similar to the previous example, only I've replaced the ^To:.* with ^TO. This is a special feature of procmail that will try to do a better job of finding out if it is really to you. In this case, it will match if the string "cunning.171@osu.edu" is found not only in the To: header, but also the CC and BCC headers. This way if someone carbon copies a message to the address, it will still get filtered.
Filtering From
:0:
* ^From.*myfriend@somewhere\.com
personal
This one will match any mail that is from "myfriend@somewhere.com". Note that in this case we didn't use a : after From. Sometimes mail servers don't put a : after the from header, so it's usually a good idea to leave it off.
Permanently deleting mail
:0:
* ^Subject:.*Nigeria /dev/null
Use this with caution, there is no way to retrieve any mail that is deleted by this filter.
