SFINX software tree - electronic mail - elm filter

What can filter do ?

Filter is a program that can do various things with incoming mail, depending on the sender, recipient or subject.

It can save mail to specified folders, run programs (with the mail message as input) or delete unwanted mail altogether. And all of this happend without any intervention of the user.

Setting up a mail filter

First of all, the filter program must be made active on incoming mail. This is done by forwarding your mail to the program. Create a .forward file in your home directory with a line like:
|/usr/local/bin/filter\ -ov /home/helium/jansen/.elm/filter.error
(of course, you should specify your own home directory.

Alternatively, you can keep a copy of your mail in your incoming mail box, and send another copy through the filter:

\jansen, |/usr/local/bin/filter\ -ov /home/helium/jansen/.elm/filter.error
And yes, this first backslash (\) is quite important. Of course you should check your filter.error file on a regular basis to see if something is going wrong.

The filter rules

The actual filtering is controlled by rules, in a file called $HOME/.elm/filter-rules

The structure of such a rule is:

if (condition) then action

Test conditions

Condition is a test, containing one of the recognized fields from the mail header:

This field is compared to a value, which is a quoted string.

Between field and value you can use a relational operator from this list (or leave it out, which is equivalent to using =):

Additional numerical operators which only work for the lines field: Besides, the filter command knows the logical operators and and not which you can use to combine relations within an if statement.

Filter actions

The actions that the filter program knows are:

Some examples

Suppose you get lots of unwanted e-mail from accounts at shoppingplanet.com (a notorious spam-site). You can delete them all if you add
if (from contains "shoppingplanet.com") then delete
If you want to be a bit more safe, you could write them to a temporary garbage file, which you check from time to time to see if anything useful was thrown in by mistake, e.g.:
if (from contains "shoppingplanet.com") then save ~/Mail/garbage

And you are subscribed to a mailinglist, and you don't want to be bothered by every mail that is coming in from that list. So you want to save these messages to a folder, which you read when ever it suits you:

if (to contains "Multiple recipients of list RULWEB") then save ~/Mail/rulweb

A final example: if your thesis-supervisor sends you an e-mail, you want to be notified directly. Let's say her user name is bigboss, so you could write:

if (from = "bigboss") then executec "echo 'Mail from bigboss' >/dev/console"
Assuming of course that you are sitting behind the console of the workstation, which you probably aren't.


ORDER: Beware: order is important in the filter-rules, since the program will not continue after it finds a matching rule. So if you have:
if (subject = "test") then save ~/Mail/mailtests
if (from = "bigboss") then save ~/Mail/frombigboss
and bigboss sends you a message with the word "test" in the subject, the message will end up in the folder =mailtests and not in =frombigboss !!!!

More examples can be found in the ELM filter manual (computer library, room 508).

Testing

It is always good to test a configuration before activating it, especially when you use the delete action.

There are two ways to do that. If you type filter -r it will read and analyze your filter rules, checking the syntax, and displaying what it is actually making of it.

You can also feed saved messages to filter to see what it should have done with them using the current rule set. To do this from elm, type | filter -n with the cursor on a message. Note that this doesn't do anything; the existing message is unaffected, and no additional copies are saved or programs executed.

Advanced options

There are some more possibilities in filter. In the commands, addresses and folders you specify in the action of a filter rule, you can make use of the following special macros: You can also use ~ to refer to your home directory in folder names (if it is the first character of course).

A different type of anti-spam filtering

If you get lots of unwanted e-mail, you can probably use a filter like this. Most of this "spam" is not explicitly addressed to you, so there's something you can test for:
#
# Anti-spam version - make sure to include all your addresses
#
if (from contains "strw.leidenuniv.nl") then leave
if (to contains "strw.leidenuniv.nl") then leave
if (to contains "jansen") then leave
if (to contains "david") then leave
# take care with our local group aliases:
if (to contains "staff") then leave
if (to contains "strwchem") then leave
if (to contains "all") then leave
always save ~/Mail/garbage
... and check the ~/Mail/garbage file on a regular basis. Some things may end up there, e.g. mailing lists. But once you identify such a message, you can always add a rule to leave such mails in your incoming mailbox.


David.Jansen@strw.leidenuniv.nl
Last modified: Thu Feb 19 16:14:40 1998