Home | About Me | Photos | Writing | Research | Scratchpad | Projects

How to filter spam with KMail and SpamAssassin

N.B. KMail in KDE 3.3 and higher has anti-spam wizards built in that set-up more or less the same filters that this document describe. You might as well use those rather than doing this manually. Find them in Tools -> Anti-Spam Wizard.

First, install KMail and SpamAssassin ;-) Then, create a mail folder called "spam", and set SpamAssassin's daemon, spamd, up to be running. You can pipe through the spamassassin binary itself, but that's much slower.

Next, create two new filters (Settings -> Configure Filters). For the first one, set it up as follows:

filter one
Match all of the following: is less than 250000
pipe through spamc
uncheck "if this filter matches, stop processing here"

Put the filter after any filters for mailing lists where you can be sure there won't be spam, so you don't have to wait for SpamAssassin to check every email. This filter will make KMail put all the emails that haven't been processed by previous filters through SpamAssassin, which will mark all those that are spam with a special flag. This flag is then picked up in the second filter. You set the size limit because SpamAssassin will choke on any messages larger than this, and in later versions will simply pass over it, thus wasting a little more processing power and time.

Set the second filter up as follows:

filter one
Match all of the following: X-Spam-Flag equals YES
move to folder spam

This filter will match all emails that have been marked as spam by SpamAssassin and move them into the folder that you created earlier. You can also move spam that wasn't detected into this folder, rather than deleting it, and then train SpamAssassin's bayseian capability once you have a few thousand spam messages.

Training SpamAssassin

Now you've got it working, and you're keeping spam that wasn't detected by SpamAssassin in your spam folder, you can train the bayseian filters like so:

sa-learn --spam --dir /home/tom/Mail/spam/cur

sa-learn --ham --dir /home/tom/Mail/inbox/cur