# HG changeset patch # User Dirk Olmes # Date 1585359795 -3600 # Node ID 94bbe517a17493bb38349db172c8b0c49ffcb5d6 # Parent ec4fc0b8cc08fe946c2ee837c7e8f4bac8fa2ba0# Parent 0e63edd28f559b2f3baf417b8e39bcf871e3aa3c Automated merge with ssh://xanthippe//home/dirk/Projekte/Blog diff -r 0e63edd28f55 -r 94bbe517a174 content/Linux/bogofilter.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/content/Linux/bogofilter.md Sat Mar 28 02:43:15 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. +