How to install and integrate SpamAssassin with Postfix on a CentOS 6 VPS

spamassassinThe following article is part 4 of our mailserver set-up with virtual users and domains using Postfix and Dovecot on a CentOS 6 VPS, followed by part 2 which explains how to install and set-up the Roundcube webmail interface on a linux vps server and then part 3 which is all about how to set-up an SSL encrypted connection in Postfix, Dovecot and Apache using SSL certificates.

In this tutorial we are going to set-up SpamAssassin on one of our CentOS 6 linux virtual servers and integrate it into our Postfix mailserver set-up so it can scan and mark the emails detected as SPAM.

What is SpamAssassin?

It is a program released under the Apache License 2.0 used for e-mail spam filtering based on content-matching rules.

UPDATE THE SYSTEM

As usual, make sure your CentOS 6 linux vps is fully up to date by executing:

## screen -U -S spamc-screen
## yum update

INSTALL SPAMASSASSIN

Install the SpamAssassin package using yum by running:

## yum install spamassassin

SET-UP USER

create spamfilter group used for the user that will run the spamassassin service

## groupadd spamfilter

create new user spamfilter with a home directory of /usr/local/spamassassin and add it to the spamfilter group you just created

## useradd -g spamfilter -s /bin/false -d /usr/local/spamassassin spamfilter
## chown spamfilter: /usr/local/spamassassin

CONFIGURE SPAMASSASSIN

next, configure spamassassin by editing /etc/mail/spamassassin/local.cf and adding/setting the following

## vim /etc/mail/spamassassin/local.cf

## Required_hits: This determines the filter balance; the lower the score the more aggressive the filter.
# A setting of 5.0 is generally effective for a small organisation or a single user.
# Adjust the strictness score to your organization's needs - a large medical organisation might want  to let email items
# through that are trying to sell pharmaceuticals, so we might increase the level to a more modest 8.0.
required_hits 5
## Report_safe:  This line determines whether to delete the item or to move the item to the inbox whilst appending
# a spam notice to the subject line. The levels for this line are set to either a 1 or 0. A score of 1 will delete the spam item,
# whereas a score of 0 will send the item to the inbox and rewrite the subject line.
report_safe 0
rewrite_header Subject [**SPAM**]
## Required_score: This line sets the spam score for all email alllowed through to your domain, with levels of certainty set from 0 to 5.
# Zero would be classified as a legitimate email item, whereas 5 would be an definite 'SPAM' item.  If we set the score to 3 we would catch a
# lot of unsolicited emails but quite a few false positives would still get through. For our example email server we will use the score of 5,
# but you can of course set this value according to your preference.
required_score 5.0

before we proceed with starting-up the spamassassin service, we need to make sure it runs with our newly created spamfilter user by editing /etc/sysconfig/spamassassin and setting-up the following:

## vim /etc/sysconfig/spamassassin

# Options to spamd
SAHOME="/usr/local/spamassassin"
SPID_DIR="/var/run/spamassassin"
SUSER="spamfilter"
SPAMDOPTIONS="-d -c -m5 --username ${SUSER} -H ${SAHOME} -s ${SAHOME}/spamfilter.log"

with all that in place, we are ready to start and enable the spamassassin service on system startup using:

## service spamassassin start
## chkconfig spamassassin on

CONFIGURE POSTFIX

next thing you need to do is to actually integrate spamassassin into postfix so it can scan and mark the emails that were detected as SPAM. To achieve this we need to edit postfix configuration file. Edit master.cf:

## vim /etc/postfix/master.cf

and add/edit the following lines:

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

then, restart postfix for the changes to take effect

## service postfix restart

TEST THE SET-UP

To test if SpamAssassin actually works, you can simply send an email with subject XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X to some of your email accounts and once the email is received, check it’s headers and you should notice something like this:

X-Spam-Flag: YES
X-Spam-Level: **************************************************
X-Spam-Status: Yes, score=1000.0 required=5.0 tests=GTUBE,RCVD_IN_DNSWL_NONE,
    TVD_SPACE_RATIO autolearn=no version=3.3.1
....

This means that SpamAssassin is doing it’s job.


Implementing SpamAssassin to the mailserver set-up with virtual users and domains using Postfix and Dovecot adds another nice feature to the mail server set-up.

However, there are still other parts missing like digital signatures using opendkim, dovecot sieve filter rules, virus scanning etc for a full-featured mail server. In the next few related articles, we will be adding additional features to the set-up so stay tuned.

Update: Part 5 – How to install and integrate OpenDKIM with Postfix on a CentOS 6 VPS

Update: Part 6 – How to set-up server-side email filtering with Dovecot Sieve and Roundcube on a CentOS 6 VPS


Of course, if you are one of our Linux VPS Hosting customers, you don’t have to do any of this, simply ask our admins, sit back and relax. Our admins will set this up for you immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

3 thoughts on “How to install and integrate SpamAssassin with Postfix on a CentOS 6 VPS

  1. Pingback: Set-up SSL encrypted connection in Postfix, Dovecot and Apache | RoseHosting.com Linux VPS Hosting Blog

    1. admin Post author

      hi el_condor,

      this set-up uses dovecot and sieve rules, not procmail. anyway, we will consider writing an article using spamassassin in combination with procmail in the near future. you can subscribe to our newsletter at http://www.rosehosting.com/blog/ so you don’t miss any future posts.

      thanks

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>