# HG changeset patch # User Dirk Olmes # Date 1585186027 -3600 # Node ID ec4fc0b8cc08fe946c2ee837c7e8f4bac8fa2ba0 # Parent fc7e1e904abaddcef7e632d7cf65c56080555610 add a blog entry for bogofilter diff -r fc7e1e904aba -r ec4fc0b8cc08 content/Linux/bogofilter.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/content/Linux/bogofilter.md Thu Mar 26 02:27:07 2020 +0100 @@ -0,0 +1,44 @@ +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. +