Skip to content

SpamAssassin deployment with Postfix — rules, training, and the action threshold

Install SpamAssassin, integrate with Postfix via spamc/amavis, train a Bayes classifier on real corpus, and pick the right score threshold for tag vs. reject.

SpamAssassin is the venerable scoring engine — a rules pack plus a Bayesian classifier plus a network lookup pack. This article is the deployment we run when a client wants spam scoring without the operational weight of Rspamd from rspamd-deploy-vs-spamassassin. Install spamassassin and spamc, wire it into Postfix through a content filter or amavis-new, pick a score threshold appropriate to the user base, and train a Bayes classifier on the client’s actual ham and spam.

How to verify

Audit current spam posture. Many Postfix installs do no scoring at all — message-level spam decisions are pushed to the client (Thunderbird filters, Outlook junk folder). That works at low volume; it does not at scale.

dpkg -l | grep -E 'spamassassin|spamc|amavis'
systemctl is-active spamassassin
ls -l /etc/spamassassin/
sudo spamassassin --lint
spamc -V

spamassassin --lint is the syntax check on rules files — any custom .cf rule with a typo will surface here. The lint should print no errors.

What’s happening

SpamAssassin is a Perl daemon (spamd) that scores each message against hundreds of rules. Each rule has a name (HEADER_FROM_DIFFERENT_DOMAINS, BAYES_99, DKIM_VALID, URIBL_BLACK) and a numeric weight. The sum is the score. A score above the configured threshold (default 5.0) gets a X-Spam-Status: Yes header injected.

The integration with Postfix has three common shapes:

  • Content filter via spamc — Postfix passes the message through spamc (a thin C client to spamd) on the way out of cleanup. Lightweight; no rewriting of envelope.
  • Milter (spamass-milter) — runs in the milter chain alongside OpenDKIM/OpenDMARC. More flexible (can reject at SMTP time) but less common.
  • amavis-new — a Perl middleware that wraps SpamAssassin, ClamAV, and various policy daemons. Heavier; useful when you need virus scanning and DLP in the same hop.

We use the spamc content filter pattern for most clients — it is the lightest, easiest to reason about, and doesn’t introduce a second daemon to monitor.

The procedure

  1. Install SpamAssassin and spamc. The package ships the daemon, the client, and the default rules pack:

    sudo apt update
    sudo apt install -y spamassassin spamc
    sudo systemctl enable --now spamassassin
    sudo systemctl status spamassassin --no-pager
  2. Enable updates and run the first rule refresh. SpamAssassin ships with stale rules; the channel updates are critical:

    sudo sed -i 's/^CRON=0/CRON=1/' /etc/default/spamassassin
    sudo sa-update -v
    sudo systemctl restart spamassassin

    The package’s cron job runs sa-update weekly thereafter.

  3. Configure global rules in /etc/spamassassin/local.cf. Set the threshold conservatively (we use 5.0 for tag, 10.0 for hard-reject), strip HTML reports, and keep headers terse:

    required_score      5.0
    report_safe         0
    rewrite_header Subject *****SPAM*****
    use_bayes           1
    bayes_auto_learn    1
    bayes_auto_learn_threshold_nonspam -0.1
    bayes_auto_learn_threshold_spam    12.0
    skip_rbl_checks     0
    dns_available       yes

    report_safe 0 keeps the original message intact and just injects headers — important because some downstream filters can’t parse a multipart spam-report wrapper.

  4. Wire the content filter into Postfix. In /etc/postfix/master.cf, change the smtp line to add -o content_filter=spamassassin:

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

    The pipe service hands the message to spamc, which forwards to spamd, gets the scored message back, and re-injects via sendmail.

  5. Reload Postfix and send a probe. The GTUBE test string forces a known spam verdict:

    sudo postfix reload
    echo "XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X" \
      | mail -s "gtube probe" your-mailbox@example.com

    The delivered message should have X-Spam-Flag: YES and a score well above 5.0. If the message arrives clean, the content filter isn’t wired.

  6. Train the Bayes classifier on real corpus. The default rules detect generic spam; Bayes adapts to your specific tenant. Collect a ham and spam corpus from the actual mailbox:

    sudo -u debian-spamd sa-learn --ham /var/vmail/example.com/alice/Maildir/cur/*
    sudo -u debian-spamd sa-learn --spam /var/vmail/example.com/alice/Maildir/.Junk/cur/*
    sudo -u debian-spamd sa-learn --dump magic

    The dump prints nspam, nham, and the database state. Aim for >200 of each before relying on Bayes scores; the classifier needs both classes balanced.

  7. Set up ongoing training via Sieve. Dovecot’s Sieve can copy mail moved to/from the Junk folder into a learning queue. The pattern:

    require ["fileinto", "imap4flags", "vnd.dovecot.pipe"];
    if header :contains "X-Spam-Flag" "YES" {
        fileinto "Junk";
    }

    Then a daily cron job runs sa-learn on whatever moved in and out of .Junk since the last run. The retraining keeps the classifier current with the tenant’s evolving mail mix.

The threshold decision

  • 5.0 tag, no reject. Conservative. Mail is tagged with X-Spam-Flag: YES and delivered; users see it in Junk. Zero false-positive reject risk.
  • 5.0 tag, 10.0 reject at SMTP. Middle ground. Tagged mail goes to Junk; clearly-spam mail (10+) is rejected with 554 5.7.1 Message detected as spam. Few legitimate messages score above 10.
  • 3.0 tag, 7.0 reject. Aggressive. Used in environments with high SaaS-marketing tolerance. Higher false-positive risk; not recommended for general business mail.

The right choice depends on what users expect from their mailbox. We default to 5.0 tag / 10.0 reject and only deviate when the client asks.

Common pitfalls

  • The Bayes database is owned by root from a manual sa-learn run, then spamd running as debian-spamd can’t update it; every message ignores Bayes and scores from rules only.
  • sa-update fails behind a corporate proxy because it uses HTTPS to ports 80 and 443; set HTTPS_PROXY in /etc/default/spamassassin or the channel updates stop coming.
  • The content filter chain doesn’t apply to mail injected via sendmail — local-pickup mail from cron jobs and mail(1) from the shell bypasses the filter unless non_smtpd_milters is wired up and you use a milter-based deploy.
  • Heavy DNS lookups (URIBL, SURBL, network tests) time out behind a flaky resolver and every score includes a 30-second wait; use a local Unbound or dnsmasq for the mail box to avoid public resolver flakiness.
  • A misconfigured required_score 0 line (sometimes copy-pasted from a “block everything” tutorial) rejects every message; always run spamassassin --lint after editing local.cf.

Stack Harbor stands up SpamAssassin as a tagging layer that lives next to DKIM/DMARC/SPF on the same box — content filter wired, Bayes trained on the client’s actual corpus, threshold picked deliberately, and the retraining loop in cron so the classifier doesn’t drift. If you have a Postfix where the spam folder is either empty or overflowing with false positives, that tuning work is part of our Managed Operations practice.