This expands on my previous post about how to set up an email server.
We’re going to set up a few spam filters in Dovecot under Debian. We’re going to use Sieve, which lets the user set up whichever filters they want. However, we’re going to run a couple pre-baked spam filters regardless of what the user sets up.
- Install Sieve.
1sudo apt-get install dovecot-sieve dovecot-managesieved - Add Sieve to Dovecot
12345678910111213141516171819202122# /etc/dovecot/dovecot.conf# Sieve and ManageSieveprotocols = $protocols sieveprotocol lmtp {mail_plugins = $mail_plugins sieve}service managesieve-login {inet_listener sieve {port = 4190}}protocol sieve {managesieve_logout_format = bytes ( in=%i : out=%o )}plugin {# Settings for the Sieve and ManageSieve pluginsieve = file:~/sieve;active=~/.dovecot.sievesieve_before = /etc/dovecot/sieve.d/sieve_dir = ~/sieve # For old version of ManageSieve#sieve_extensions = +vnd.dovecot.filter#sieve_plugins = sieve_extprograms} - Install and update SpamAssassin, a heuristic perl script for spam filtering.
12sudo apt-get install spamasssassinsudo sa-update
123# /etc/default/spamassassinENABLED=1#CRON=1 # Update automatically
12# /etc/spamassassin/local.cfreport_safe 0 # Don't modify headers
1sudo service spamassassin start - There’s a lot of custom configuration and training you should do to get SpamAssassin to accurately categorize what you consider spam. I’m including a minimal amount here. The following will train SpamAssassin system-wide based on what users sort into spam folders.
12345678#!/bin/sh# /etc/cron.daily/spamassassin-trainall_folders() {find /var/mail/vmail -type d -regextype posix-extended -regex '.*/cur|new$'}all_folders | grep "Spam" | sa-learn --spam -f - >/dev/null 2>/dev/nullall_folders | grep -v "Spam" | sa-learn --ham -f - >/dev/null 2>/dev/null - Make Postfix run SpamAssassin as a filter, so that it can add headers as mail comes in.
12345# /etc/postfix/master.cfsmtp inet n - - - - smtpd-o content_filter=spamassassin# ...spamassassin unix - n n - - pipe user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
1sudo service postfix restart - Add SpamAssassin to Sieve. Dovecot (via Sieve) will now move messages with spam headers from SpamAssassin to your spam folder. Make sure you have a “Spam” folder and that it’s set to autosubscribe.
12345678# /etc/dovecot/sieve.d/spam-assassin.sieverequire ["fileinto"];# Move spam to spam folderif header :contains "X-Spam-Flag" "YES" {fileinto "Spam";# Stop here - if there are other rules, ignore them for spam messagesstop;}
12cd /etc/dovecot/sieve.dsudo sievec spam-assassin.sieve - Restart Dovecot
1sudo service dovecot restart - Test spam. The GTUBE is designed to definitely get rejected. Set the content of your email to this:
1XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X - You should also be able to create user-defined filters in Sieve, via the ManageSieve protocol. I tested this using a Sieve thunderbird extension. You’re on your own here.