Subject: Filtering mail with deliver.

From: [email-masked] (Jim Davis)
Date: 27 May 1994 17:18:07 -0700

It looks like questions about how to sort incoming mail into separate
folders are going to be pretty common.  Currently there isn't a way in
pine itself, but you can use an auxillary program to work with pine
and get mail automatically sorted into different folders.  Here's one
way, using Chip Salzenberg's "deliver" program to do the sorting, on a
UNIX system using sendmail as the mail transport agent.

First of all you'll need to get deliver installed on your system, if
it's not already.  It's usually installed setuid root, so that will
take cooperation from your system administrator.  (Though the
installation notes say that you can install it in a personal directory
and do without root-setuidness.)  The source code to deliver is
available from any major ftp archive; use archie to find one near you.
Deliver should compile and run on most UNIX systems without any
problems.

Once deliver is installed, say in /usr/local/bin/deliver, then you
tell sendmail to pass all incoming mail through deliver by putting

"|/usr/local/bin/deliver your-username"

into your .forward file in your home directory.  (The quotes are
needed, and don't forget to add your user name as an argument or
sendmail may do unintuitive and unpopular things to your mail.)

Now what deliver does with your mail depends on the the rules inside a
file called .deliver in your home directory.  Here's (a shortened
version of) my .deliver file:

#!/bin/sh
user="$1"
TO=`header -f To -f CC $HEADER`

case "$TO" in
*clarissa*)	echo $user:mail/clarissa;;
esac

case "$TO" in
*usenet*) echo $user:mail/news; exit;;
esac

# Default case
echo $user

It's a Bourne shell script that looks at the 'To:' (or 'Cc:') line in
the header of the incoming mail message.  If the message came from the
Clarissa Explains it All mailing list, then the 'To:' line says

To: [email-masked]

and that matches the '*clarissa*' pattern in the first case statement.
So that message is put in my clarissa folder.  If the message came
from our Usenet software, then the 'To:' line says

To: usenet

and that matches the '*usenet*' pattern in the second case statement.
So that message goes to my news folder.

The last line in the .deliver file sends a copy of everything to my
inbox, so anything else besides the Clarissa or news mail goes to my
inbox as usual, and a copy of the Clarissa mail goes to my inbox too.
That way I can see everything in one place, and still have a copy put
in the right folder automatically.  (Since the news mail is usually
uninteresting I bypass the copy to my inbox with the 'exit' command at
the end of that case statement.)

Deliver can do much more but this should give you an idea of how it
can be used to sort incoming mail.  The .deliver file itself doesn't
have to be a Bourne shell script; anything that has the effect of
writing the user name or a file pathname to standard output would do.