Filter spam to a spam folder on the server

I’ve recently moved my web hosting package from a managed Lycos package to a virtual server with Webfusion. One of the main reasons for this was that Lycos offered no Spam filtering at all. I was constantly getting 40-50 Spam emails every night. If I was only using Outlook this wouldn’t be too much of an issue as that has spam filtering built in, but I tend to access my email from my mobile phone, or from a web browser. Neither offer any spam mail filtering. Lycos enable you to use Squirrel Mail, but they have the Spamassassin plugin disabled and there is no way of enabling it.

With Webfusion I can pretty much do what I want as I have full root access to my server. After configuring Spamassassin I set about using filters in Horde, this was a massive improvement, I finally had spam being filtered. This still left the issue of my mobile phone which would still download all spam emails. (Fortunatley I’m on an inclusive data tariff).

So I set about getting my new server to filter Spam emails as they arrived into a Spam folder. After searching around on Google for a bit, I came across this article:

http://www.chrisbeach.co.uk/view/tech/mail_filtering_using_procmail_in_qmail_
under_plesk

The first thing to do is setup the spam folder in your email. This process will be different depending on the email client you use, I’ll leave you to work out this one! I’m assuming from this point on that your spam folder will be called ‘Spam’.

The next step is to setup procmail, qmail and safecat. I had the first 2, but had to install safecat. The above link describes how to get hold of safecat and compile it, but I found that the following did the trick:

apt-get install safecat

The next step is to setup procmail. To do this, create the following file:

/var/qmail/mailnames/chrisbeach.co.uk/chris/.procmailrc

Replacing the domain and username to suit your requirements. The contents of the file should be as follows:

MAILDIR=/var/qmail/mailnames/chrisbeach.co.uk/chris/Maildir
SPAMDIR=${MAILDIR}/.Spam
DEFAULT=${MAILDIR}/
LOGFILE=${MAILDIR}/procmail.log
LOG="--- Logging ${LOGFILE} for ${LOGNAME} "
All mail tagged as spam
# (eg. with a score higher than the set threshold)
# is moved to the designated spam folder
:0
* ^X-Spam-Status: Yes.*
| /usr/local/bin/safecat "${MAILDIR}/tmp" "${SPAMDIR}/new" 

Replacing .Spam on line two to match your Spam folder, and changing the domain name and username as appropriate.

Before continuing to the next step, note down the owner and group of .qmail. To do this issue the following command (assuming you are already in the same folder as .procmailrc):

ls -l .qmail

You’ll get something similar to this:

-rw------- 1 popuser popuser 77 Apr  9 09:21 .qmail

Note down the owner, the first username, the first popuser, and the group, the second popuser.

The next step is to modify the file .qmail (it’s in the same folder as .procmailrc), making sure that you have taken a backup of the original file, you have been warned! The contents of .qmail should be:

| /opt/psa/bin/psa-spamc accept
|preline /usr/bin/procmail -m -o .procmailrc

Note:- the first line in my file differs from the file detailed in the article by Chris Beach, this is obviously to do with your installation of Plesk, you will have to check where psa-spamc is installed. One way to do this would be to issue the following commands:

cd /
find / -name psa-spamc

This will return the directory where psa-spamc is installed.

The last thing to check is the file permissions for .procmailrc and .qmail. This is critical, it needs to be right, I didn’t do this bit and lost a couple of emails! I’m not sure if all steps are necessary, but it works for me: (use the owner and group recorded earlier, in my case both were popuser)

chmod 600 .qmail
chown popuser .qmail
chgrp popuser .qmail
chmod 600 .procmailrc
chown popuser .procmailrc
chgrp popuser .procmailrc

That just about finishes it. Let me know if you have any issues, but most of all, make sure you backup your original settings!

Thanks to Chris Beach for the original article, of which I have borrowed heavily!

Leave a Reply