How to Setup SpamAssassin With Postfix On Ubuntu 16.04

By Francis Ndungu, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Unsolicited emails are very annoying because they take up your disk space and waste your time every time you try to weed them out from your inbox . Luckily, SpamAssassin is one of the best Open-source tools for filtering and blocking spam.

Designed in Perl, Apache SpamAssassin uses different approaches to identify and block spam. These include: online blacklist database lookup, checksum based filters, Bayesian algorithm and third party programs.

The highly configurable email filter utility is rule-based and to function effectively, it uses regular expressions to determine if an email contains Spam.

If you have already setup an email server with Postfix, SpamAssassin is a good tool for keeping your inbox free from unwanted emails from hackers and telemarketers.

This is a comprehensive guide that walks you through the process of securing your Ubuntu 16.04 Postfix email server with SpamAssassin.


  1. An Alibaba Cloud account.
  2. An Alibaba Cloud ECS instance running Ubuntu 16.04 Linux distribution.
  3. Postfix Email Server. See our guide for setting up an email server with Postfix, Dovecot and SquirrelMail on Ubuntu 16.04 Alibaba ECS
  4. A non-root user that can perform sudo tasks on your server.

Step 1: Installing and Configuring SpamAssassin

SpamAssassin is available on the Ubuntu software repository. So before we install it, let’s first update the package information index:

Then we can install SpamAssassin with the command below:

Step 2: Creating a User for SpamAssassin

Next, we need to create a dedicated user for SpamAssassin:

Step 3: Configuring SpamAssassin

After installation, we are going to edit the SpamAssassin configuration file. To do this, open the file /etc/default/spamassassin using nano text editor:

To enable SpamAssassin on the system, find the parameter ENABLED and change its value from 0 to 1.

Next, we are going to create a SAHOME variable and direct it to SpamAssassin home directory:

Then, locate the below line on the same file:

Change it to:

To pick the latest updates from SpamAssassin, we are going to change the CRON value from 0 to 1:

When you are done editing the file, press CTRL+X, Y and Enter to save the changes.

Step 4: Adding Rules to SpamAssassin

We can now start adding rules to SpamAssassin. Open the file /etc/spamassassin/

Using nano text editor:

Make sure the below parameters and values are set on the file:

The above parameters tell SpamAssassin to rewrite the subject of each email that surpasses the threshold score of 5.0. When set to 1, use_bayes instructs SpamAssassin to use the Bayes algorithm to classify emails as either ham or spam using a mathematical formula based on probability.

The parameter bayes_auto_learn is set to 1 to enable the auto-learning mechanism. Save and close the file by pressing CTRL+X, Y and Enter.

Step 5: Configuring Postfix to Use SpamAssassin as an Email Filter

Next, edit the Postfix master configuration file and instruct it to route emails to SpamAssassin for filtering:

Find the line:

Then, add the following information below it:

Save and close the file by pressing CTRL+X, Y and Enter. Then, restart both Postfix and SpamAssassin.

Step 6: Testing SpamAssassin Email Filter

To check whether SpamAssassin is working, send an email to your server (e.g. and add include the content below on the email body to trigger the spam check:

Open your email client (in this case, we are using SquirrelMail) and check your inbox. Your email should now be marked as spam and the subject should be re-written to indicate the same.


We have gone through the steps of securing your Postfix email server with SpamAssassin on Ubuntu 16.04 server hosted on Alibaba Cloud. We believe you are going to implement the steps in this article to safeguard your email servers from unwanted emails.

If you haven’t setup your email server, remember to check out our guide on How to Setup Email Server with Postfix, Dovecot, DirectMail and SquirrelMail on Ubuntu 16.04 Alibaba ECS


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store