jYopp.com

Maildrop and Spamassassin

02/17/2008 12:54am

I have used SpamAssassin before, but the junk mail filtering in OS X's mail client (which I use) is good enough that I didn't bother reconfiguring everything when I switched mail servers. However, now that I have the iPod Touch, I find myself awash in a flood of SPAM unless my home computer is awake and connected to the server at all times. I need spam filtering on the server again.

After looking through a number of tutorials and manual pages, I wrote this script for Courier's maildroprc, which is serving me beautifully:

xfilter "spamc -u vmail"

/^X-Spam-Status: *Yes. *score=([0-9.]+)/:h
SPAMSCORE=$MATCH1

LOGDIR="/var/log"
logfile "$LOGDIR/deliverylog"

if ($SPAMSCORE > 20)
{
	/^From:.*/:h
	log "(Score $SPAMSCORE) Destroyed Message $MATCH"
	exit
}
else
{
	if ($SPAMSCORE >= 5)
	{
		to "$DEFAULT/.Spam/"
	}
	else
	{
		to "$DEFAULT"
	}
}

This script may not be exactly what you want for your own mail, but it serves as a near-perfect example of how to deliver mail in three tiers: Definitely SPAM, Almost certainly SPAM, and Normal. My server uses global configurations for SpamAssassin; If you want per-user spam settings, each user must have a valid UNIX account on the machine. Replace the first line with "spamc -u $LOGNAME".

It's worth noting that the $SPAMSCORE variable will be NULL unless the message is classified as spam by SpamAssassin. Comparing it to values lower than SpamAssassin's threshold is useless, so if you want to work with borderline cases, you may need to lower SpamAssassin's threshold or modify the regular expression for Spam-Status.