New user's registration have been closed due to high spamming and low trafic on this forum. Please contact forum admins directly if you need an account. Thanks !

Mini howto: Filter incoming e-mails using procmail

A collection of tips on howto tweak your Bubba.
Post Reply
fallberg
Posts: 23
Joined: 06 Jun 2009, 12:25

Mini howto: Filter incoming e-mails using procmail

Post by fallberg »

Hello,
I have found some discussions on this forum regarding email filtering and thought I should give procmail a shot.
I ran across some minor issues when I tried this but they were easily solved and I thought I should share the experience.
First the disclaimer: This howto requires SSH and root access to the Bubba. Do not do attempt this unless you know what you are doing as it may mess up the bubba and/or throw away the incoming emails unless properly configured.

So, here goes:
Install procmail (as root):

Code: Select all

apt-get install procmail
Create a configuration in your home-root (as "yourself"):

Code: Select all

nano .procmailrc
The .procmailrc file contains the filters and configuration procmail will use when fetchmail pipes your mail to procmail (more on this later).
Here is a sample .procmailrc:

Code: Select all

SHELL=/bin/sh
PATH=/usr/bin:/usr/local/bin
MAILDIR=$HOME/Mail/
DEFAULT=$MAILDIR
PMDIR=$HOME/.pmlog
LOGFILE=$PMDIR/pmlog
VERBOSE=yes

:0: # Facebook
* ^From.*facebook
$DEFAULT.INBOX.Personal.Facebook/

:0: # Newsletters
* ^From.*opencores|\
  ^From.*avanza|\
  ^From.*dustin|\
  ^From.*avagonow|\
  ^From.*youtube|\
  ^From.*flysas
$DEFAULT.INBOX.Personal.News/

:0: # All other mail go into INBOX
$DEFAULT
This configuration tells procmail that my mail is located in the 'Mail' folder on my home partition (default).
It also tels procmail to store the activity log in a file called 'pmlog' in the folder '.pmlog' in my home (you might have to create the folder fist, not sure about that)
I also tells it to be "verboseive" as that gives me more info in the log on the filtering results which is useful when debugging the filtering.

I have two filters configured. One will grab all facebook mail and store them in a subfolder I have created in my Inbox called "Personal/Facebook". Since Bubba uses maildir format for mails, it is important to add the last '/' to the path or else procmail will assume mbox format and store your mails "incorrectly".

I also have a bigger rule for all mail I consider news, and any hit on the OR:ed list of addresses will send the mail to my "Personal/News" folder.
The rules use reg-exp:s and you can of course set up pretty sophisticated rules here, but this is out of the scope of this howto. Google is good if you want to learn more about "procmail recipies".

My last rule basically takes all mail that fell through the previous rules and put them in my inbox.

When you have finished editing .procmailrc make sure the permissions on the file is good for procmail to use.
I had to change it in order to make it work:

Code: Select all

chmod 640 ~/.procmailrc
Now it is time to tell fetchmail that it should send the mails to procmail.
This is done by adding this string after the configuration(s) you want to enable procmail for in /etc/fetchmailrc (as root):
mda "/usr/bin/procmail -d %s"
A sample fetchmailrc for gmail retrieval could look something like this:

Code: Select all

set postmaster "postmaster"
set bouncemail
set no spambounce
set properties ""
set daemon 300
set syslog

poll imap.gmail.com with proto IMAP
        user 'account@gmail.com' there with password 'secret' is 'bubba-user' here ssl mda "/usr/bin/procmail -d %s"
Note that the mda-string is on the same line as 'ssl'.

Replace account with your gmail username and replace secret with the password you use on gmail. Finally replace 'bubba-user' with the bubba user you want to receive the mail.
The last mda-string will tell procmail that it should parse the .procmailrc file in the 'bubba-user' home directory when determining where the mail should go.

Fetchmail will automatically detect changes to /etc/fetchmailrc so the next scheduled poll should now send the mails through the procmail filtering. And this enables server-side mailfiltering also for IMAP.

Make sure you get the recipies right in .procmailrc or procmail might throw away your mail (for me, it sent them to /var/mail/<user> when I messed up my config)

I hope you find the mini-howto useful. Feel free to add any improvements and/or corrections.

Best regards,
Patrick Fallberg
Last edited by fallberg on 21 Jul 2009, 05:44, edited 1 time in total.
johannes
Posts: 1470
Joined: 31 Dec 2006, 07:12
Location: Sweden
Contact:

Re: Mini howto: Filter incoming e-mails using procmail

Post by johannes »

(Moved topic to the Howto section)

Thanks a lot, we do appreciate you sharing stuff like this!
/Johannes
/Johannes (Excito co-founder a long time ago, but now I'm just Johannes)
fallberg
Posts: 23
Joined: 06 Jun 2009, 12:25

Re: Mini howto: Filter incoming e-mails using procmail

Post by fallberg »

Thanks. Noticed a little mistake in the newsletters rule and corrected it (no * on every ORed rule).
Post Reply