Log inSign up

Filter spam on a Postfix server with SpamAssassin

Install spamassassin with the spamd daemon and the spamc client, add a pipe service to Postfix master.cf that hands each incoming message to spamc, and set required_score in /etc/spamassassin/local.cf. Suspicious mail then arrives with X-Spam-Flag: YES, which a Dovecot Sieve rule moves to Junk. As on any mail server here, confirm outgoing port 25 with a support ticket first and keep an authenticated relay on port 587 for sending.

Topic: Email servers · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13 · Updated 2026-12-03

Check the sending side before adding a filter

A spam filter only helps a mail server that works, so settle outbound delivery first. Open a ticket with support and ask whether outgoing port 25 is open for your instance; it is not guaranteed. The dependable alternative is to relay outgoing mail through an authenticated SMTP service on port 587, which the relay guide sets up in a few lines. Ask in the same ticket for a PTR record on your IP addresses, because reverse DNS is set by support.

This page assumes the Postfix and Dovecot server from Postfix, Dovecot and Roundcube by hand, on Ubuntu 24.04, Ubuntu 22.04, Debian 12 or Debian 13. If you used iRedMail, spam scoring is already wired in through Amavisd and you should not add a second filter.

How much memory does SpamAssassin need?

Plan for roughly 150 to 300 MB for the daemon and its child processes, more with many concurrent messages. On A1-2 keep the number of children low (the default of five is fine for personal mail); on A2-4 you have room for a busier domain plus webmail. SpamAssassin runs its rules in Perl, so CPU spikes briefly for each message, which a shared vCPU handles well at small volumes.

Install the daemon and client

Since version 4.0 the Debian and Ubuntu packages split the daemon into its own package called spamd.

sudo apt update
sudo apt install spamassassin spamc spamd     # Ubuntu 24.04, Debian 12 and 13
sudo apt install spamassassin spamc           # Ubuntu 22.04 (3.4 series, daemon included)
spamassassin --version
apt policy spamassassin

Enable and start the service. Its unit is spamd on the 4.x packages and spamassassin on Ubuntu 22.04:

sudo systemctl enable --now spamd            # 4.x
sudo systemctl enable --now spamassassin     # Ubuntu 22.04
sudo ss -tlnp | grep 783

The daemon listens on 127.0.0.1:783 only, which is exactly what you want: nothing outside the server should reach it.

Which settings belong in local.cf?

Keep site settings in /etc/spamassassin/local.cf. A compact starting point:

required_score     5.0
rewrite_header     Subject [SPAM]
report_safe        0
use_bayes          1
bayes_auto_learn   1
trusted_networks   127.0.0.1 198.51.100.24

report_safe 0 keeps the original message intact and only adds headers, which is gentler for users. A score of 5.0 is the long standing default; lower it to 4.0 after a few weeks if spam still slips through, or raise it if real mail gets tagged. Validate the file and restart:

sudo spamassassin --lint
sudo systemctl restart spamd

No output from --lint means the configuration is valid.

How do I connect SpamAssassin to Postfix?

Define a pipe transport that runs spamc and reinjects the scored message, then tell the public SMTP listener to use it as a content filter. The debian-spamd user is created by the package.

sudo postconf -M spamassassin/unix='spamassassin unix - n n - - pipe user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}'
sudo postconf -P "smtp/inet/content_filter=spamassassin:dummy"
sudo postfix check
sudo systemctl restart postfix
sudo postconf -M | grep -E "^(smtp|spamassassin) "

Only port 25 gets the filter. Mail your own users submit on 587 skips it, which saves resources and avoids tagging outgoing messages. If spamd is down, spamc -f passes the message through untouched instead of losing it.

Move tagged mail into a Junk folder

Have Dovecot deliver the mail so a Sieve rule can file spam away. Install the Sieve plugin and switch Postfix from direct Maildir delivery to the Dovecot delivery agent:

sudo apt install dovecot-sieve
sudo postconf -e 'mailbox_command = /usr/lib/dovecot/dovecot-lda -f "$SENDER" -a "$RECIPIENT"'
sudo mkdir -p /etc/dovecot/sieve
sudo tee /etc/dovecot/sieve/spam-to-junk.sieve >/dev/null <<'EOF'
require ["fileinto", "mailbox"];
if header :contains "X-Spam-Flag" "YES" {
  fileinto :create "Junk";
  stop;
}
EOF
sudo tee /etc/dovecot/conf.d/98-sieve.conf >/dev/null <<'EOF'
protocol lda {
  mail_plugins = $mail_plugins sieve
}
plugin {
  sieve_before = /etc/dovecot/sieve/spam-to-junk.sieve
}
EOF
sudo sievec /etc/dovecot/sieve/spam-to-junk.sieve
sudo systemctl restart dovecot postfix

The quoted EOF keeps the shell from expanding $mail_plugins. Users still see the Junk folder in Roundcube or any IMAP client and can rescue a false positive with one drag. On Debian 13 the Dovecot 2.4 syntax differs, so adapt the second file with the 2.4 documentation.

How can I confirm a message was scored?

Send the GTUBE test pattern, which every SpamAssassin install marks as spam on purpose.

printf "Subject: gtube test\n\nXJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X\n" | sendmail deploy@example.net
sudo grep spamd /var/log/mail.log | tail -n 5
ls ~deploy/Maildir/.Junk/new/

The log shows a line such as identified spam (1000.0/5.0), and the message lands in the Junk folder. Local mail sent with sendmail does not pass the port 25 filter, so for this test send from an outside mailbox if nothing appears; then open a normal message and look for X-Spam-Status: No, score= in its headers.

Keep the rules fresh and train Bayes

Rule updates arrive through sa-update. The 4.x packages run it from a systemd timer; check that it is scheduled, or run it by hand:

systemctl list-timers | grep -i spam
sudo sa-update && sudo systemctl restart spamd

Bayesian learning improves results quickly once it has seen a few hundred examples of each kind. Feed it from real folders, then fix ownership so the daemon user can read the database:

sudo sa-learn --dbpath /var/lib/spamassassin/.spamassassin --spam /home/deploy/Maildir/.Junk/cur
sudo sa-learn --dbpath /var/lib/spamassassin/.spamassassin --ham /home/deploy/Maildir/cur
sudo chown -R debian-spamd: /var/lib/spamassassin/.spamassassin
sudo sa-learn --dbpath /var/lib/spamassassin/.spamassassin --dump magic | head

Pair the filter with proper sender checks on your own domain (SPF, DKIM and DMARC) so your outgoing mail is not judged harshly elsewhere.

What if legitimate mail is being tagged?

Read the score breakdown first: the X-Spam-Status header lists every rule that fired. Allow a trusted sender with whitelist_from (or welcomelist_from on 4.x) in local.cf, train the message as ham with sa-learn --ham, or lower a single rule with score RULE_NAME 0.5. Restart spamd after any change and run --lint again.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2personal mail with light traffic1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4a busier domain with webmail on the same server2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about SpamAssassin with Postfix

Does SpamAssassin block mail or only mark it?

With this setup it marks it. Nothing is rejected; tagged messages go to Junk, so a false positive is never lost.

Should I scan outgoing mail too?

Usually not. Your own users authenticate on 587, which skips the filter. Outgoing delivery is better served by a relay on 587 and correct DNS records.

Is port 25 needed for SpamAssassin to work?

Inbound 25 brings mail in for scoring. For outbound, ask support by ticket whether port 25 is open, and use an authenticated relay on 587 if it is not.

Can I use Rspamd instead?

Yes, Rspamd is a separate, faster filter with its own web interface. Run one filter, not both, on the same Postfix.

How often should sa-update run?

Once a day is plenty. The packaged timer handles it; a manual run is harmless if you want fresh rules immediately.