server-side mail filtering using qmail/procmail/safecat under plesk
After enabling Spam Assassin on my Plesk-managed domain I had been relying on email clients to move spam-tagged emails into a "Spam" folder. This was impractical as it only worked when a client was open, and rules had to be set up on each client.
After a lot of digging on Google I worked out a server-side solution. The key app here is procmail, which runs rules on incoming mails and can file them into folders. Procmail should already be present on your Plesk installation. Another app called safecat is required to place filtered mail into a maildir within the file system. The following steps apply to my mailbox "chris" on domain "chrisbeach.co.uk" You will obviously need to substitute your own mailbox and domain.
1. Create a folder called "Spam" in your mailbox.
2. Install safecat (see http://jeenyus.net/~budney/linux/software/safecat.html)
cd /tmp/
wget http://jeenyus.net/~budney/linux/software/safecat/safecat-1.13.tar.gz
tar xvzf safecat-1.13.tar.gz
cd safecat-1.13
make
make setup check
3. Create /var/qmail/mailnames/chrisbeach.co.uk/chris/.procmailrc
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"
4. Edit /var/qmail/mailnames/chrisbeach.co.uk/chris/.qmail so that it contains only the following two lines:
| /usr/local/psa/bin/psa-spamc accept
|preline /usr/bin/procmail -m -o .procmailrc
Let me know how you get on, and also if you know of any enhancements I could make to these instructions
01/02/07 02:46pm
(5 years ago)




Procmail newer as 3.14 is able to deliver to Maildirs as well:
MAILDIR=/var/qmail/mailnames/xxx/yyy/Maildir
DEFAULT=${MAILDIR}/
SPAMDIR=${MAILDIR}/.Junk/
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.*
${SPAMDIR}
07:57pm
Have you considered using sa-learn in conjunction with SpamAssassin?
More information at: http://spamassassin.apache.org/full/3.1.x/doc/sa-learn.html
03:51pm
Hi,
I've used saper's way and it worked.
Here's my .procmailrc file:
MAILDIR=/var/qmail/mailnames/domain.ro/cristi/Maildir
DEFAULT=${MAILDIR}/
SPAMDIR=${MAILDIR}/.Spam/
TRASHDIR=${MAILDIR}/.Trash/
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.*
${SPAMDIR}
I've created the logfile, chown to vpopuser:vpopuser, and the lof should look like this:
--- Logging /var/qmail/mailnames/domain.ro/cristi/Maildir/procmail.log for popuser From publicitate@domain.ro Thu Dec 31 16:42:10 2009
Subject: ****SPAM**** HIGH * Visitor publicitate's personal 80% OFF
Folder: /var/qmail/mailnames/domain.ro/cristi/Maildir/.Spam/new/126 4300
.
--
Thank you!
04:51pm
Hi Chris,
Thanks for this, very insightful. Using your post and others. I've put together a similar solution (excluding safecat) which automatically sorts this out for all mail accounts on plesk automatically, whilst still maintaining some form of control via the CP. Hope it helps others like you helped us out:
http://blog.netweblogic.com/linux/mail/filter-spam-folder-procmail-qmail-plesk/
Thanks
nwl
10:10am
heres the .procmailrc I used (plesk, ubuntu, qmail)
MAILDIR=/var/qmail/mailnames/XXX.com/james.paterson/Maildir
SPAMDIR=${MAILDIR}/.Junk/
DEFAULT=${MAILDIR}/
LOGFILE=${MAILDIR}/procmail.log
LOG="--- Logging ${LOGFILE} for ${LOGNAME}"
:0
#All mail tagged as spam is moved to the designated folder
* ^X-Spam-Status: Yes
${SPAMDIR}
:0
* ^X-Spam-Status: YES
${SPAMDIR}
and heres the .qmail:
| true
| spamc | preline /usr/bin/procmail -m -o .procmailrc
Good luck!
12:46pm
I used the solution posted by "saper" and it did work (thanks).
BUT webmail won't read the mail which is in the spam folder. I *do* have mails in the spam folder but they just won't show up in the folder of the webmail client (horde)
Does anyone have a good idea why?
Thanks
03:17pm
I have been trying to get SA to work under Plesk for many hours now.
This did the trick.
I tried saper's suggestion to use .procmailrc without safecat and it did not function properly on my install (even though my procmail is 3.22)
My ISP wanted $6.95 a month the enable SA. When they "installed" SA it did not function properly in Plesk. That set me off of trying to figure out what was wrong, so I had them "uninstall" SA in Plesk. I am grateful that they hosed my install because after much searching (and help from a friend that compiled safecat for me on his local machine), now SA is functioning properly and we don't have to pay a dime.
Thanks for the post!
01:52am
Hi for me it dosen't work.
--- Logging for popuser procmail: Skipped "* ^X-Spam-Status: Yes.*"
safecat: fatal: could not stat directory: file does not exist
procmail: Error while writing to "/usr/bin/safecat"
procmail: Lock failure on "/var/qmail/mailnames/domain.es/pruebas/Maildir/LOGFILE=/var/qmail/mailnames/domain.es/pruebas/Maildir/procmail.log.lock"
procmail: Error while writing to "/var/qmail/mailnames/domain.es/pruebas/Maildir/LOGFILE=/var/qmail/mailnames/domain.es/pruebas/Maildir/procmail.log"
07:33pm
I have been wanting this for a while.
Thanks!
07:10am