It is possible to do server-side mail filtering of Litech mail on marduk using Exim. This filtering can be used instead of or in addition to any client-side filtering that is done by Outlook, Netscape, or another mail client. The advantage of doing the filtering on the server is that all filtering is performed as soon as the message is received, which is often within seconds of the message leaving the sender's email system. Your email client does not have to be reading mail for the processing to work.
Another advantage to filtering is that you can append a "suffix" to your email address and use this to filter your mail. For example, I subscribed to daily Infobeat news updates with the address firstname.lastname@example.org. Exim will identify this address as mine and pass the message to my filter script. My script is set up to recognize the "-infobeat" on the end of my address and handle Infobeat messages in a special way.
Filtering is specified by writing an Exim filter file and storing it named .forward in your marduk home directory. The complete specification for the format is on the Exim site, but here is a short tutorial/example and some information specific to our setup. The last section gives some commands for testing the filter file.
# Exim filter
This line is critical! It designates that the rest of the file is in Exim-filter format instead of in the traditional format expected of .forward files. Capitalization and spacing don't matter and you can place any text you like on the line after the word "filter", but I recommend using this line as-is.
Blank lines are ignored. Anything after a # sign is considered a comment and is ignored (except for the first line).
There are some variables available to compare and test, and they are referenced with a $ sign in front of their name. If the variable name is not followed by whitespace, you need to enclose the name of the variable in curly-braces. Here are some of the variables:
All of the message's headers are stuffed into special variables. The names are composed of the header tag (e.g. "Subject:") in lower-case, prepended with "header_". For example, $header_subject: contains the subject-line from the message. Note that the colon after the tag in the variable name is optional.
These are some of the message-processing commands.
To test conditions, use one or more of these:
# Exim filter
Don't forget that line!
if error_message then finish endif
This statement halts processing if it is running on an error message. Since error messages are pretty important (usually indicating a delivery error for a message your sent), we stop processing to make sure they don't accidentally get lost. I recommend that you check for error messages at the very beginning of your filter file.
if $local_part_suffix is -infobeat then save infobeat-news elif $local_part_suffix is -junk or $local_part_suffix is -spam then seen finish endif
Two conditions have been sequenced here using the "elif" form of the "if" command. Messages to email@example.com (assuming that I am using this filter file) will be saved into a file named /home/lutchann/infobeat-news and messages to firstname.lastname@example.org or email@example.com will be thrown away.
if $sender_address ends @cornell.edu then deliver firstname.lastname@example.org endif
Any mail that comes from a person at Cornell will be forwarded to my email address there instead of being delivered locally.
if $header_subject contains "urgent" then seen finish endif
This throws away all messages with the word "urgent" in the subject line.
Since our IMAP server uses Maildir-format mailboxes, you'll need to use a special format if you want messages to be saved directly to folders that can be accessed with your mail client. All mail is stored in your Maildir folder, and subdirectories are preceeded with a period. The path for a folder named "November-2000" inside a folder named "Classes" would then be "Maildir/.Classes/.November-2000". When you're specifying a path like this in a "save" command, don't forget to add the slash to the end to specify that it is a Maildir mailbox. A save command for this path would look like this:
Exim will not deliver to directories that do not already exist, so make sure you create a folder with the appropriate name before you set up your filter file!
Of course, testing your filter file before enabling it is always a good idea. To do this, make sure you create your file initially with a different name (like, say, forward-test) and then only rename it to .forward after you've checked that it works the way you want.
To test, you'll need to have a message saved to a file that Exim will filter. You can find lots of test messages in your Maildir/cur directory. I'll assume you've named it test-message and that it's in the current directory. Test the filter file with a command like this:
$ sendmail -bf [filter file] < [test message file]
$ sendmail -bf forward-test < test-message
If you're testing filtering on sender, you need to specify the appropriate address with a -f flag:
$ sendmail -bf forward-test -f email@example.com < test-message
If you're testing filtering on a suffix, you need to specify that too:
$ sendmail -bf forward-test -bfs -foo < test-message
If you want to do anything more complicated, or even if you don't, I recommend you read the full filtering documentation on the Exim site.