www.ntnu.no *
INFOWEB >  email >  general  > Basic email sorting and filtering with procmail
Tasks
You may add comments
Or e-mail this article
Read articles written by thomasan
User rating
How much did this article help you?
Much Some Little
Print preview
You do not need a 'printer-friendly page' to print this page. Here's why
Contents

Basic email sorting and filtering with procmail

Why sort and filter your email?

Sorting and filtering of email is useful to:

General info

On the stud servers you can filter your email with the program procmail. This program is activated automatically every time you receive an email if you follow the instructions below.

Instructions

Create a file in the root folder of your home directory with the name .procmailrc with any editor. This will be the configuration file for procmail and needs to have the following in the start of the file.:

kode #1
MAILDIR=$HOME/mail
LOGFILE=$MAILDIR/.procmail-log

Many procmail users have already defined variables like HOME, DEFAULT and SHELL. You should not define these yourself, as procmail defines them automatically.

Example

In this example you want to filer out email that comes to the mail list maillist@list.stud.ntnu.no to its own mailbox maillist Also, you want to filter out email that comes from a friend "friend@stud.ntnu.no" to the mailbox "Friends". That means you need two filter rules:

kode #2
:0 H:
* ^TO.*maillist@list.stud.ntnu.no
maillist

:0 H:
* ^From:.*friend@stud.ntnu.no
friends

All email that is marked with To:/Cc: maillist on list.stud.ntnu.no will now be put in the mailbox maillist. The mailbox is created when you first receive a mail that triggers the rule. The mailbox will be in the directory mail together with the main mailbox INBOX.

If you use ^To not ^TO it will not include carbon copy (Cc:) mail. You can also filter on e.g. ^Subject:

NB! The symbol in the first line is the number zero, not capital 'o'.

Filtering out spam

To do additional filtering of spam other than the already default spamassainsin based filtering that is run on stud you should read the following article: "Spam filtering with procmail".

Finished example file

The following file you can download and save on the root of your home directory. Lines that begin with #, is discarded by procmail as comments(they are ignored).

Finished example of a .procmailrc (has to be renamed as .procmailrc to work).

What can go wrong?

If you write this file in windows (e.g. with notepad) it may not work. This is because a good deal of the windows based text editors add a control symbol at the end of each line to show that there shall be a line break here. You can edit the files with a UNIX editor and remove the symbols. They look like this: ^M. If you use Textpad you can choose to save it with "File Format" set to UNIX and it will work.

When you forward mail with procmail it is very important to know that you are set as sender when it forwards. If the email does not reach the recipient it will be returned to you. If this is not caught by a rule in procmail is may try to resend the returned mail to the already failed recipient. This will result in an never-ending loop.

The easiest way to prevent this form happening is to include the following rule as the first rule in .procmailrc:

kode #3
:0:
* ^FROM_MAILER
$DEFAULT

This rule will catch all error messages (which is sent from a special user called MAILER-DAEMON or other senders as procmail recognizes as this special user) and put those messages directly in the inbox. This prevents never-ending loops which may cause instability on the mail servers.

Learn more?

For more information about procmail you can log in to login.stud.ntnu.no and type man procmail, man procmailrc and man procmailex. Also this Procmail FAQ is very useful.


Languages: en flag_no Comments: 0 Aid: 330 User rating:
 
 
Last updated: 2007-07-02 Author: thomasan Read: 2901 times