How to stop receiving spam emails on Linux with SpamAssassin

How to stop Receiving Spam Emails on Linux with SpamAssassin

In the ongoing struggle against unwanted communication there are many technologies and strategies on how to stop receiving spam emails. One highly effective way for Linux users is the open-source Apache SpamAssassin project, offering a highly-effective and free anti-spam platform.

In this article, we’ll teach you exactly how to stop receiving spam emails on Linux with SpamAssassin.

What is SpamAssassin and how it works

Spamassassin is a tool that scores spam emails by comparing them against various tests. It uses a header (X-Spam-Status:) marked with a number ranging from 0-5, with a setting of 3.5 being safe enough to reduce false positives and the amount of SPAM that makes it through. A SPAM score is calculated from multiple characteristics, ranging from 0 to 5, and has been effective for years. Spamassassin is a helpful tool for filtering spam emails. In this tutorial, we’ll learn how to integrate this filter into our Postfix server.

Prerequisites

  • An up-to-date Linux system
  • Postfix installed on your system
  • Root access to your server

Installing SpamAssassin

The installation of SpamAssassin service is easy to do. We accomplish it with the following command:

sudo apt-get install spamassassin # Ubuntu
sudo yum install spamassassin # AlmaLinux

After installing the service, we must integrate it with the Postfix mail server. To accomplish that, we’ll follow the next topic.

Configuring Spamassassin with Postfix

The main configuration file from Spamassassin is generally located at /etc/spamassassin/local.cf. There, you’ll find the required_score part, which sets what is spam and what is not. You can keep it as 5 and uncomment the line if it’s commented:

required_score 5.0

If you need to whitelist someone’s domain, you can add the following directive to your local.cf file:

whitelist_from *@sender_domain.com

Also, we need to configure report_safe. This directive will do the handle of the marked spam messages.

report_safe 0 # The incoming SPAM messages will have the header modified with a directive pointing it's spam
report_safe 1 # The incoming messages tagged as SPAM will create a report and attach the original message to it
report_safe 2 # The incoming SPAM will be attached as a new report and sent as plain text

To identify spam messages on the mailbox, we need to remove the rewrite_header Subject comment line so it will identify the spam:

rewrite_header Subject SPAM

And it’s done the configuration. Now, we need to integrate Spamassassin and Postfix.

Integrating Postfix and SpamAssassin

The postfix configuration file is located at /etc/postfix/master.cf. The first change required to be done is related to the SMTP line, so the line would look like:

smtp inet n - - - - smtpd

Then, after our changes, with the integration of SpamAssassin, it would look like this:

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

At the end of our file, we need to add the following content:

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

That’s it. SpamAssassin is configured on your system and integrated with Postfix. At this point, you can test the email deliverability of your service to see if it’s working as expected. If you’re struggling with this, feel free to contact our Incident Server Support team, and we’ll help you get everything sorted out.

Leave a Reply

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