Pim de Greef - student psychologie

Setting up SpamAssassin

Setting up SpamAssassin is fairly easy. It is a free and open-source SPAM filter written in Perl from the same makers as Apache. This tutorial was made for Ubuntu 18.04 but should work on all (not too old) Ubuntu and Debian versions.

Postfix needs to be installed first.


Let’s begin with the installation.
apt-get install spamassassin spamc

And add a user.
adduser spamd --disabled-login

Configuration of SpamAssassin

Edit the configuration with your favorite editor at /etc/default/spamassassin

OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir /home/spamd/ -s /home/spamd/spamd.log"

Then edit /etc/spamassassin/local.cf to set up SPAM rules

rewrite_header Subject ***** SPAM _SCORE_ *****
report_safe             0
required_score          5.0
use_bayes               1
use_bayes_rules         1
bayes_auto_learn        1
skip_rbl_checks         0
use_razor2              0
use_dcc                 0
use_pyzor               0

Configuration of Postfix

Edit /etc/postfix/master.cf to add a content filter to the SMTP server

smtp      inet  n       -       -       -       -       smtpd
    -o content_filter=spamassassin
spamassassin unix -     n       n       -       -       pipe
    user=spamd argv=/usr/bin/spamc -f -e  
    /usr/sbin/sendmail -oi -f ${sender} ${recipient}

For the changes to take effect, use the following commands:

systemctl restart postfix.service
systemctl enable spamassassin.service
systemctl start spamassassin.service

Optional: train SpamAssassin with sa-learn

You can train SpamAssassin to learn what is SPAM or not (also known as ‘ham’) in your particular situation. You do this with the sa-learn command. It is said that it works the best if you let it scan 3000+ ham or SPAM mails but let’s start with what we’ve got. Use it regularly until all or most of the SPAM doesn’t reach your inbox but your spam folder and if that is set be sure to use it once in a while. Be sure to train (an equal amount of) both spam or ham or SpamAssassin wil be biased towards SPAM if you only let it train on that.

Be careful with this commands so you don’t let it think valid mails are SPAM or vice versa! Make sure your inbox is free of SPAM.

The basic command to train SPAM (for the mbox format) is:
sa-learn --no-sync --spam --mbox ~/mail/spam

This will scan all the mail in the folder /home/user/mail/spam and remember it to be SPAM. To train ham you can use the following commands:
sa-learn --no-sync --ham --mbox ~/mail/mailinglists or
sa-learn --no-sync --ham --mbox ~/mail/family

The inbox in the mbox format is mostly found at /var/spool/mail/[user]

After that you’ll need to let sa-learn rebuild it’s index and clean up after itself with
sa-learn --sync

If you want to see what is in the database now, use
sa-learn --dump magic

The command for the mbx format is:
sa-learn --no-sync [--spam or --ham] --mbx [folder]

the inbox would presumably be reached with:
sa-learn --no-sync --ham --mbx INBOX

And the command for the Maildir format is:
sa-learn --no-sync [--spam or --ham] [folder/{cur,new}]

for example:
sa-learn --no-sync --spam /folder/.INBOX.Spam/{cur,new}

sa-learn --no-sync --ham /folder/.INBOX.family/{cur,new}

This should cover it. Good luck with the setup! Questions? Go to contact or leave a comment.