Processing Email With Procmail

Posted on April 26th, 2019

           

Procmail is one of the best mail processing utility, it provides better email filtering than Thunderbird, and is normal with command line applications, it is fast. It can handle both POP and IMAP protocols. Procmail is a Mail Delivery Agent (MDA),  it can be used along with a Mail Transfer Agent (MTA) such as mutt or sendmail to filter messages. The main difference it have from others as, it process all messages before they delivered to your mailbox.

To start with procmail on the desktop, we need to create two important files , they are .procmailrc and .forward on your home directory.  The .forward file makes all incoming messages run through procmail, and the .procmailrc used to specifies the settings such as location of mailboxes and the mail filtering rules.

Your .forward file must include the line |/usr/bin/procmail. The symbol ” | ” will pipes all incoming mails to procmail. A basic .procmailrc might look like this:

# .procmailrc file

PATH=/usr/sbin:/usr/bin

MAILDIR=$HOME/mail  #location of your mailboxes

 

The PATH on the file tells the procmail where to look for  programs, such as sendmail, and MAILDIR is the location of your mailboxes. Which means, if you have any directory under the home location and having another directories inside it, for any purpose related to mail such as where procmail delivers messages. Now we already informed that procmailrc used for rules setting, so for that there are three parts to procmail recipe.

First, :0 indicates the start of a recipe.

Second, * refers to the start of the condition for filtering messages.

Third, the action line specifies the name of the folder where messages should be delivered if the condition is true ( not in the case of false ).

Example: The .procmailrc file, which use to filter messages from  friends and a couple of mailing lists.

# .procmailrc file

PATH=/usr/sbin:/usr/bin

MAILDIR=$HOME/mail  #location of your mailboxes

DEFAULT=$HOME/mail/priority  #any mail that is not filtered into some other folder gets here

#The recipes begin

#these are my friends

:0

* ^From:.*(linuxlala\@yahoo\.co\.in|shashankjee@gmail\.com)

$MAILDIR/buddies

#separate directories for my mailing lists

:0

* ^TOcc-licenses@lists\.ibiblio\.org

$MAILDIR/cc-license

:0

* ^TOindlinux-group@lists\.sourceforge\.net

$MAILDIR/indlinux

# default rule, drops messages into the default box

:0

* .*

priority

 

On the above file, you can add extra comments that you wish to add anywhere in your file except in the condition lines.

1) The folder $MAILDIR/buddies here specifying the exact location where we need to drop the messages from the specified one. The one of the advantages on the procmail is that, we don’t need to elaborate the exact path of folder where it need to go when writing recipes.  If you want to make all the messages to go on buddies, the other option directly specify the path as buddies instead of $MAILDIR/buddies.

2) The ^TO is one of the special expression in procmail used for creating filters for mailing lists. Please ensure that there is no space between the email address you wish to filter and ^TO.

 

You can filter spam using procmail. Here is an example:

#sort spam

:0

* ^Subject:.*(offer|debt|free|training|cash)

garbage or /dev/null

 

If you wish to delete permanently those messages that contains the mentioned words in the subject, you can use /dev/null instead of garbage. The deleted messages can not be retrieved.

 

If you need any further help, please do reach our support department.

 

Leave a Reply