SpamAssassin analyzes incoming mail content and characteristics to score and filter spam — a mature, widely-used content-based filtering layer that complements SPF/DKIM/DMARC and greylisting.
How SpamAssassin Works
Analyzes each message against many rules (content patterns, header anomalies, sender reputation checks) and assigns a spam score — messages exceeding a configured threshold are flagged or rejected, with the specific handling configurable.
Step 1 — Install SpamAssassin
sudo apt install spamassassin spamc -y
Step 2 — Create a Dedicated User
sudo adduser --system --group spamd
Step 3 — Enable and Configure SpamAssassin
sudo nano /etc/default/spamassassin
ENABLED=1
OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir /home/spamd -s /var/log/spamassassin.log"
Step 4 — Start SpamAssassin
sudo systemctl enable --now spamassassin
Step 5 — Integrate with Postfix
sudo nano /etc/postfix/master.cf
smtp inet n - y - - smtpd
-o content_filter=spamassassin
spamassassin unix - n n - - pipe
user=spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
Step 6 — Restart Postfix
sudo systemctl restart postfix
Configuring Spam Score Thresholds
sudo nano /etc/spamassassin/local.cf
required_score 5.0
rewrite_header Subject [SPAM]
Messages scoring above the threshold get their subject line tagged — letting users' mail clients filter tagged messages into a spam folder based on this marker.
Setting Up Automatic Spam Folder Filing (Sieve, with Dovecot)
require "fileinto";
if header :contains "X-Spam-Flag" "YES" {
fileinto "Junk";
}
A Sieve rule (processed by Dovecot) that automatically files SpamAssassin-flagged mail into a Junk folder, rather than relying solely on subject line tagging.
Training SpamAssassin with Bayesian Filtering
sa-learn --spam /path/to/spam/messages
sa-learn --ham /path/to/legitimate/messages
Training SpamAssassin on your actual mail improves accuracy over time — feed it known spam and known legitimate (ham) examples specific to your mail patterns.
Updating SpamAssassin's Rule Sets
sudo sa-update
Rule sets are regularly updated as spam techniques evolve — schedule this periodically (via cron) to keep filtering effectiveness current.
Balancing False Positives and False Negatives
Too aggressive a threshold causes legitimate mail to be flagged (false positives, genuinely costly if important mail is missed); too lenient lets spam through — tune the threshold based on observed accuracy for your specific mail patterns, and monitor for user complaints about missed legitimate mail.
Common Errors
All mail suddenly flagged as spam — check for a recent rule update causing overly aggressive scoring, or verify your own server's IP/domain hasn't developed a reputation issue affecting its own scoring somehow.
Continue Reading
- How to Set Up Greylisting to Reduce Spam
- How to Set Up ClamAV for Email Virus Scanning
- How to Troubleshoot Email Not Sending or Going to Spam
Browse more articles in Email Hosting & Deliverability.