Adding Spam Protection to Postfix

If you’ve followed my first tutorial, you already have a fully functional email server.  Unfortunately, where there is email, there is also spam.  We can easily add some protection against unwanted messages using some tools in Postfix as well as adding SpamAssassin, SPF checking and optionally, greylisting.

First lets install SpamAssassin: [1]

apt-get install spamassassin spamc libmail-dkim-perl

Now lets add a new user for SpamAssassin, and create a few directories for it to use:

groupadd spamd
useradd -g spamd -s /bin/false -d /var/log/spamassassin spamd
mkdir /var/log/spamassassin
chown spamd:spamd /var/log/spamassassin

Now we can configure SpamAssassin:

nano /etc/default/spamassassin

Replace ENABLED=0 with ENABLED=1 and replace CRON=0 with CRON=1.

Next, create a new line:


Now, find the OPTIONS= line and replace it with:

OPTIONS="--create-prefs --max-children 2 --username spamd -H ${SAHOME} -s ${SAHOME}spamd.log"

Exit nano by pressing CTRL-X and press Y to save your changes.

Now lets configure SpamAssassin’s rules:

nano /etc/spamassassin/

Edit and uncomment the following lines to look like: [2]

report_safe 0
required_score 3.0
use_bayes 1
bayes_auto_learn 1

Now add the line:

skip_rbl_checks 1

This will stop SpamAssassin from checking blacklist servers, as we will be adding this to Postfix a little later.  There is no point in checking twice as it adds unnecessary extra processing on your server and wastes bandwidth for the blacklist providers.

Exit nano saving your changes.  Now we can (re)start SpamAssissin:

service spamassassin restart

Next we’ll be configuring Postfix to use SpamAssissin:

nano /etc/postfix/

The first uncommented line should look like:

smtp     inet     n     -     -     -     -     smtpd

Add -o content_filter=spamassassin just below that line, so it looks like:

smtp     inet     n     -     -     -     -     smtpd
  -o content_filter=spamassassin

Notice the space before -o? You need to make sure there is a space between smtpd and the lines you have just added, otherwise Postfix will report an error.

Page down to the bottom of the file and add these lines:

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

Exit nano and save your changes.

Now restart Postfix to apply your changes:

service postfix restart

Now is a good time to stop and test your changes.  Send an outbound email and send an incoming email to your server.  You can check the headers of the messages you send a receive to make sure SpamAssassin is adding headers to you messages.  You can also check the SpamAssassin log file:

nano /var/log/spamassassin/spamd.log


Adding DNSBL (Blacklists), Rate Limiting & Other Protection

Next we’re going configure Postfix to check two blacklists before receiving messages, as well as adding some other protection.  We go back to configuring Postfix:

nano /etc/postfix/

Find the line smtpd_recipient_restrictions and edit it to look like: [3]

smtpd_recipient_restrictions =

And add the following lines: [4] [5]

disable_vrfy_command = yes
smtpd_helo_required = yes
smtpd_helo_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_non_fqdn_helo_hostname, reject_invalid_helo_hostname
strict_rfc821_envelopes = yes
smtpd_error_sleep_time = 1s
smtpd_soft_error_limit = 10
smtpd_hard_error_limit = 20
smtpd_client_connection_count_limit = 10
smtpd_client_connection_rate_limit = 60

These lines add checks to Postfix, making sure that the hostnames connecting to your mail server actually exist and are valid.  As mentioned before, we have also added in two blacklist checks here.  This prevents sites that have been blacklisted from even sending their messages to your server, saving processing power and bandwidth.

The two blacklists I have selected are the Spamhaus Zen list and Spamcop‘s list.  Spamhaus seems to block the majority of spam and SpamCop blocks most of the rest. If something does make it through, you are able to report these messages to SpamCop and they will be blocked in the future.  Zarafa is configured by default to put any message that is marked as spam in the Junk E-Mail folder.

We’ve also added rate limiting here, so that spamming mail servers don’t flood your server with connections.

Blacklists are a great and effective way of blocking a large amount of spam.  Occasionally a legitimate email server can find itself added to blacklist, this can happen for a variety of reasons.  You can request to be added to a whitelist which can help this problem.  You can also create your own whitelist with servers you know you can trust.

To create our own whitelist: [6]

nano /etc/postfix/rbl_override

Add the IP addresses or hostnames of mail servers you trust: OK OK OK

Exit and save your changes. Now run: (You will also need to run this command each time you modify this file)

postmap /etc/postfix/rbl_override

Now we tell Postfix to use this whitelist:

nano /etc/postfix/

We will need to add the line check_client_access hash:/etc/postfix/rbl_override to smtpd_recipient_restrictions after reject_unauth_destination and just before the blacklist checks:

smtpd_recipient_restrictions =
  check_client_access hash:/etc/postfix/rbl_override,

After any change we make in Postfix, we need to reload to apply our changes:

service postfix reload


Adding SPF (Sender Policy Framework) Checking

Next we will add SPF (Sender Policy Framework) checking to Postfix: [7]

apt-get install postfix-policyd-spf-python

Back to editing

nano /etc/postfix/

Add this line:

policy-spf_time_limit = 3600s

Now add check_policy_service unix:private/policy-spf to smtpd_recipient_restrictions just before the whitelist line we just added:

smtpd_recipient_restrictions =
  check_policy_service unix:private/policy-spf,
  check_client_access hash:/etc/postfix/rbl_override,

Next we edit /etc/postfix/

nano /etc/postfix/

Add this section to the bottom of the file:

policy-spf  unix  -       n       n       -       -       spawn
  user=nobody argv=/usr/bin/policyd-spf

And reload Postfix:

service postfix reload

We can test to make sure SPF checking is working by using the Wormly SMTP Server Test. Try sending a test message to your server, but use as the sender email. You should see your server reject the message as Wormly is not authorized to send email messages on behalf of

You should make sure you have a valid SPF record for your domain in your DNS server. If you are just using your new Digital Ocean VPS, your SPF record could be as simple as "v=spf1 mx ~all". This will help prevent spammers from spoofing messages using your domain name.  For more information on creating a SPF record, visit SPF Wizard.  For more information on SPF in general, visit OpenSPF.


[Optional] Greylisting with postgrey

We already have blacklists and whitelists, why not add one more list into the mix?

A greylist works by delaying mail from untrusted servers.  If the sending mail server is standards-compliant, it will try resending the message, and then the server will accept it. Spam mailers do not typically try resending a message, and so the spam is blocked. Servers that do try resending mail will be added to postgrey’s white list, and will not be delayed in future. In other words, the first message from any given sender will be delayed, but subsequent ones will not.

This approach may not be appropriate for everyone. If you need to receive messages in a timely manner, you may not want to install postgrey.  I have come across certain servers that only retry sending messages every four hours.  Servers you have added to your own whitelist listed above will not be greylisted.

First we need to install postgrey: [8]

apt-get install postgrey

The default wait time for postgrey is 5 minutes, lets bring that down to one:

nano /etc/default/postgrey

Add --delay=60 to the end of POSTGREY_OPTS=:

POSTGREY_OPTS="--inet=10023 --delay=60"

Now to add postgrey to Postfix, we edit

nano /etc/postfix/

Add the line check_policy_service inet: to smtpd_recipient_restrictions just before permit:

smtpd_recipient_restrictions =
  check_policy_service unix:private/policy-spf,
  check_client_access hash:/etc/postfix/rbl_override,
  check_policy_service inet:,

Now restart both postgrey and Postfix:

service postgrey restart
service postfix restart

Head over to the Wormly SMTP Test once again and try to send a test message to your server. The message should fail. Checking the reason on the Wormly page, you should see Greylisted as the reason.  If you wait one or two minutes and then try again, you’ll see that the message will go through.

You can also check your logs:

tail /var/log/mail.log
Jul 10 13:23:53 hostname postgrey[4656]: action=greylist, reason=new,,,,
Jul 10 13:24:30 hostname postgrey[4656]: action=greylist, reason=early-retry (23s missing),,,,
Jul 10 13:24:58 hostname postgrey[4656]: action=pass, reason=triplet found, delay=65,,,,


And that should do it.  Using this configuration, I receive very little spam in my Junk E-Mail folder.  Those that do manage to make their way through, I promptly report to SpamCop and they usually stop rather quickly.

In my next tutorial we will be adding SSL/TLS protection to the webmail interface, Postfix and the POP/IMAP interfaces.  We will also be adding DKIM signing to outgoing messages.

As always, if you notice any errors, omissions or have anything to add that would make this tutorial better, please let me know in the comments below.


Leave a Reply

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