view content/Linux/bogofilter.md @ 110:be0331916375

README
author Dirk Olmes <dirk.olmes@codedo.de>
date Fri, 18 Jun 2021 07:24:40 +0200
parents ec4fc0b8cc08
children
line wrap: on
line source

Title: Filtering spam mails with bogofilter
Date: 2020-03-22
Lang: en

I'm running a [Postfix](http://www.postfix.org/) server here at home to process incoming mail. To integrate spam filtering I'm delivering mails via [procmail](https://en.wikipedia.org/wiki/Procmail) - it's as simple as setting `mailbox_command = /usr/bin/procmail` in postfix' `main.cf`.

Spam filtering works by invoking the filter ([bogofilter](https://bogofilter.sourceforge.io/) in my case) from procmail like this:

	:::shell
	: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:

	:::shell
	: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:

	:::shell
	#!/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.