How to train Spam Assassin and Bogofilter from Cyrus IMAP folders

I use Cyrus IMAP to access my email and I run bogofilter and Spam Assassin on it. Both of these need training to be really effective, and while I had a quick and dirty solution it wasn't really easy to use.

After a fair amount of frustration banging my head against Perl I ended up with this script. It works by logging into the IMAP server as an admin (I made one just for this task - you need to specify the username in imapd.conf), and checking all the users to see if they have any spam or ham to be learnt. It then downloads these messages, forks, drops privs and runs as the user in question and calls sa-learn and bogofilter on the messages, once that is done it deletes them.

If you want to use it have a look at the configuration section to tweak it for your setup (ie what you want to call the Spam/Ham folders, who your admin user is). I strongly suggest you don't use root as your admin user because you need to keep a copy of that user's password around to login.. In any case the file you put the password in should be owned by root and have permissions of 0600.

While you are debugging it I suggest turning debugging on (duh) and running it by hand a few times. Once you are happy with it you can turn debugging off and run it from cron. It is designed to hold the output from sa-learn and bogofilter and log it on an error. On a clean exit it will print nothing so you will only see emails from cron when something has broken.


Daniel O'Connor
Last modified: Mon Dec 12 14:46:22 CST 2005