Filtering spam mails with bogofilter

22.03.2020 by Dirk Olmes

I’m running a Postfix server here at home to process incoming mail. To integrate spam filtering I’m delivering mails via procmail - it’s as simple as setting mailbox_command = /usr/bin/procmail in postfix’ main.cf.

Spam filtering works by invoking the filter (bogofilter in my case) from procmail like this:

:0 fw
| /usr/bin/bogofilter -uep

This command will pipe the mail content through bogofilter which will add a X-Bogosity header denoting the spam status of the mail.

A second procmail rule moves all spam to a designated spam folder on my IMAP server:

:0
* ^X-Bogosity: Spam.*
$HOME/.maildir/.INBOX.Spam/

For training I use two designated folders on my IMAP server: LearnSpam and LearnGood. I’m simply moving mails that I want to train as spam to the LearnSpam folder. Once per hour I train spam/good mails using this script:

#!/bin/bash
SPAM_FOLDER=$HOME/.maildir/.INBOX.Spam.LearnSpam
if [[ -d $SPAM_FOLDER ]]; then
        SPAM_CUR=$SPAM_FOLDER/cur
        # only invoke bogofilter if there are mails in the folder
        if [[ "$(ls -A $SPAM_CUR)" ]]; then
                /usr/bin/bogofilter -s -B $SPAM_FOLDER && rm $SPAM_CUR/*
        fi
fi
HAM_FOLDER=$HOME/.maildir/.INBOX.Spam.LearnGood
if [[ -d $HAM_FOLDER ]]; then
        HAM_CUR=$HAM_FOLDER/cur
        # only invoke bogofilter if there are mails in the folder
        if [[ "$(ls -A $HAM_CUR)" ]]; then
                /usr/bin/bogofilter -n -B $HAM_FOLDER && rm $HAM_CUR/*
        fi
fi

The script is invoked via cron.


Comments

There are no comments yet.

Leave a comment
Your name:
Comment: