Unix Help
Procmail: Automagically Sorting Your Incoming Mail


Procmail is a utility that can be used to perform many different (and powerful) functions. One of these many functions is to use procmail to sort your incoming mail into different mailbox folders. The steps shown below have been written assuming:
  1. Your user name is `justme'.
  2. Your preferred email editor is `PINE'.
  3. The utility, `procmail', is installed on your mail server.
  4. The full path to `procmail' on your mail server is `/usr/pkgs/procmail/bin/'.
  5. You receive email from someone whose email address is `jim@host.name.edu'.
  6. You wish all of these incoming email messages to be automagically routed to a mail folder called `jimsmail'.
To do this, proceed as follows:
  1. move to your home area with the command:

    >> cd

    Note: you do not type the ">>" characters. They are shown above in order to mimic the UNIX prompt.

  2. Using whatever text editor you prefer, create/edit a `.forward' file. For example, if you use the `vi' editor, you would enter the command:

    >> vi .forward

    Note: The dot character in front of the name, `forward', is part of the file's name. By naming a file with a leading dot character, the file becomes "hidden", and will not be seen by some UNIX utilities and commands.

  3. Enter the following line AT THE TOP of your `.forward' file:

    "|IFS=' ' && exec /usr/pkgs/procmail/bin/procmail -f- || exit 75 #justme"

    Note: The single quotes, double quotes, and # character are to be entered EXACTLY as they are shown. The easiest way to make sure that the syntax of the line you type is correct, is to NOT type it at all. Simply cut/paste it from this page. Then edit the line. You MIGHT need to edit the path to `procmail'. You will DEFINITELY need to change the user name, `justme', to your user name.

  4. After saving the `.forward' file, change it's permission to allow world readability with the command:

    >> chmod 644 .forward

  5. Now create a "hidden" directory for procmail to use for storing error messages. Do this with the command:

    >> mkdir .procmail-logs

    Note: Don't forget the leading dot character in front of the file-name.

  6. Now create a `.procmailrc' file. Using the `vi' editor, you would enter the command:

    >> vi .procmailrc

  7. In the `.procmailrc' file, enter the following lines:

    # .procmailrc
    # routes incoming mail to appropriate mailbox folders
    VERBOSE=OFF
    MAILDIR=$HOME/mail
    LOGDIR=$HOME/.procmail-logs
    LOGFILE=$LOGDIR/log.`date +%y-%m-%d`

    :0:
    * ^From:.jim@host.name.edu
    jimsmail


    Note: Again the syntax of the lines are critical to your setup working properly. As before, the easiest way to assure that you get the correct syntax is to cut/paste the above lines to your `.procmailrc' file and edit the necessary items.

    As stated earlier, all of the examples assume that you are using PINE as your email editor. PINE stores its mail in a directory called `$HOME/mail'. If you use a different email editor, the directory for mail storage may be different. For example, `mh' stores its mail in a directory called $HOME/Mail. If you are using an email editor that stores mail to a different directory, you will need to edit the line that specifies `MAILDIR'.

    The last three lines are the ones that do the work (and the ones you'll need to edit). Simply change the email address, `jim@host.name.edu', to whatever email address you're trying to trap, and change the mail box folder name, `jimsmail', to whatever name you wish your folder to have.

    A Few More Examples:

    You can sort different types of mail based on different criterion into desired mail box folders simply by adding more intructions into your `.procmailrc' file. Consider the examples below:

    1. # .procmailrc
      # routes incoming mail to appropriate mailbox folders
      VERBOSE=OFF
      MAILDIR=$HOME/mail
      LOGDIR=$HOME/.procmail-logs
      LOGFILE=$LOGDIR/log.`date +%y-%m-%d`

      :0:
      * ^From:.jim@host.name.edu
      jimsmail

      :0:
      * ^From:.*jane
      janesmail


      Now, in addition to all mail from `jim@host.name.edu' being sorted into the `jimsmail' folder, all mail from ANYONE who has the text, `jane', ANYWHERE in their email address, will be sorted into the mail box folder, `janesmail'. For example, if you received email from any one of the following email addresses, all of it would be sorted into the folder, janesmail:

      jane@this.host.edu
      janet@that.host.edu
      john@janes.host.edu
      sam@host.janell.edu


    2. # .procmailrc
      # routes incoming mail to appropriate mailbox folders
      VERBOSE=OFF
      MAILDIR=$HOME/mail
      LOGDIR=$HOME/.procmail-logs
      LOGFILE=$LOGDIR/log.`date +%y-%m-%d`

      :0:
      * ^From:.jim@host.name.edu
      jimsmail

      :0:
      * ^From:.*jane
      janesmail

      :0:
      * ^To:.*admin
      workmail


      You may be a member of a group mailing list. If you are, the email that you receive will not have your own email address in the `To:' field of the mail header. Rather, the `To:' field will the contain the name of the mailing list of which you are a member.

      In the above example, all mail that you receieve that has the text, `admin', ANYWHERE in the `To:' field of the email address, will be sorted into the mail box folder, `workmail'.

    3. # .procmailrc
      # routes incoming mail to appropriate mailbox folders
      VERBOSE=OFF
      MAILDIR=$HOME/mail
      LOGDIR=$HOME/.procmail-logs
      LOGFILE=$LOGDIR/log.`date +%y-%m-%d`

      :0:
      * ^From:.jim@host.name.edu
      jimsmail

      :0:
      * ^From:.*jane
      janesmail

      :0:
      * ^To:.*admin
      workmail

      :0:
      * ^Subject:.*joke
      junkmail


      All of us have been recepients of `jokes' email. In many cases, we don't even know how they ended up in our email box. This last example will take all incoming mail with the text, `joke', anywhere in the subject, and sort it into the mail box folder, `junkmail'.

    For more information about `procmail' and how to use it, refer to the links below:

    Filtering Mail FAQ
    Mail Filtering with Procmail