How to Block Spam Email with Apache SpamAssassin on Ubuntu 16.04

Apache SpamAssassin is an open source enterprise-class spam filter that can be used by mail server admins to block spam, also known as unsolicited bulk email. Written in Perl, SpamAssassin is developed by the Apache software foundation. In this tutorial, we will take a look at how to install and configure SpamAssassin on Ubuntu 16.04, integrate it with Postfix SMTP server and the methods that we can use to fight spam emails.

Spams have undoubtly wasted many hours of our lives and sometimes make us angry. You probably received spams of the following kinds:

  • Some Africans asking help with bank wealth transfer
  • Product pitching emails from Chinese
  • One night stand invitations
  • Get rich quick schemes
  • and on and on

To follow this tutorial, it’s assumed that you have already installed Postfix on Ubuntu 16.04. If not, please check out the following step-by-step guide.

SpamAssassin can use the following tests on incoming emails:

  • Header fields
  • Body phrase identification
  • Bayesian filtering
  • Automatic white/blacklist
  • Manual white/blacklist
  • Character set and locales

Legitimate emails are called ham by SpamAssassin.

Install and Configure Apache SpamAssassin

SpamAssassin is avaiable from Ubuntu software repository. Install SpamAssassin using the following command. spamc is the client for SpamAssassin spam filtering daemon.

sudo apt install spamassassin spamc

Then create a user named spamd.

sudo adduser spamd --disabled-login

Edit SpamAssassin configuration file.

sudo nano /etc/default/spamassassin

Find the following line:

OPTIONS="--create-prefs --max-children 5 --helper-home-dir"

We specify the username Spamassassin will run under as spamd and set the location of the log filename as /var/log/spamd.log.

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

To enable the cron job to automatically update spamassassin’s rules on a nightly basis, find the line

CRON=0

And change it to

CRON=1

Save and close the file.

Set up SpamAssassin Rules

Edit /etc/spamassassin/local.cf file.

sudo nano /etc/spamassassin/local.cf

All lines in this file are commented out by default. You can read the file to see what each rule does. The following settings are used on my mail server.

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

Save and close the file. Then start spamd by issuing:

sudo systemctl start spamassassin.service

And enable autostart at boot time:

sudo systemctl enable spamassassin.service

spamd will be listening on TCP port 783 for local host.

Passing Incoming Emails from Postfix to SpamAssassin

Edit Postfix master configuration file.

sudo nano /etc/postfix/master.cf

Find the following line:

smtp      inet  n       -       -       -       -       smtpd

And add the following option to it.

-o content_filter=spamassassin

You can also add the following lines to the end of the file in order to enable after-queue content filter.

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

Save and close the file. Then reload Postfix for the changes to take effect.

sudo systemctl reload postfix

Now your anti-spam system should be working.

That’s it!

I hope this tutorial helped you install and set up Apache SpamAssassin on Ubuntu 16.04. As always, if you found this post useful, then subscribe to our free newsletter.You can also follow us on Google+Twitter or like our Facebook page.

Rate this tutorial
[Total: 1 Average: 5]