Add Custom SpamAssassin Rules to Zimbra Without Editing Shipped Files

To add a local SpamAssassin rule in a current Zimbra installation, place it in Zimbra’s SpamAssassin local-rules directory, validate the syntax, and restart Amavis on the MTA host so the scanner reloads it. For ZCS 8.5 and later, Zimbra documents /opt/zimbra/data/spamassassin/localrules for custom rules. Older releases use a different layout, so check your Zimbra version before editing files.

Where the rule belongs

Zimbra’s documentation describes three SpamAssassin directories for ZCS 8.5 and later: localrules for local customizations, rules for the shipped rule set, and state for downloaded or compiled rule data. Put your own .cf file in localrules; do not edit files under /opt/zimbra/data/spamassassin/rules, because those files belong to the product’s rule set and may be replaced by updates.

Custom rules must be installed on every Zimbra server that runs the MTA and Amavis content-filtering services for inbound mail. In a multi-MTA deployment, a file on one server does not automatically appear on the others. Use the same reviewed rule set on each relevant host.

A dark file browser panel showing a folder tree and a list of configuration files
The rule file belongs in Zimbra’s SpamAssassin local-rules area on each MTA host.

Write a small rule and assign a cautious score

Run commands as the zimbra account. Create a separate file so the rule is easy to review, disable, or remove without disturbing other local settings:

su - zimbra
cd /opt/zimbra/data/spamassassin/localrules
vi 60_local_subject.cf

For a harmless example, the following rule adds points when the subject contains the phrase “limited time offer,” regardless of capitalization:

header LOCAL_LIMITED_OFFER Subject =~ /limited time offer/i
score LOCAL_LIMITED_OFFER 2.0
describe LOCAL_LIMITED_OFFER Subject contains a targeted phrase

SpamAssassin uses a test name to connect the rule, score, and description. The header directive tests a message header with a regular expression; score sets how much the match contributes to the message’s total spam score; and describe makes the test recognizable in reports. Use a distinctive test name made of letters, digits, and underscores. Avoid broad matches such as a single common word, and do not start with a large score or a negative score that effectively whitelists messages.

For message-body matching, use the body directive. For example, a rule can inspect message text for a distinctive phrase. The text that SpamAssassin evaluates can include decoded text parts and the subject, depending on the rule type and configuration. Test against representative messages before applying a score that could materially affect delivery.

A dark code editor panel with several short colored rule-like lines
A local rule file keeps the match, score, and description together for review.

Validate before loading the rule

Check the configuration before restarting Amavis:

/opt/zimbra/common/bin/spamassassin --lint

Zimbra’s Spamhaus setup instructions use this command to check SpamAssassin configuration. If lint reports a parse error, correct the line it identifies and run the check again. Common causes include a misspelled directive, malformed regular-expression delimiters, a mismatched rule name between header and score, or editing a file that is not in the active local-rules directory.

Do not restart services while the rule still fails validation. Keep a copy of the previous file before changing an existing ruleset. If a recent addition is the only difference and lint fails, move that new file out of localrules or restore its prior contents, then lint again.

A dark terminal-style panel with grouped command and list rows
Run SpamAssassin’s lint check after saving the file and before restarting Amavis.

Restart Amavis and test with a controlled message

After lint passes, restart the Amavis service on the MTA host as the zimbra user:

zmamavisdctl restart

This reloads the content-filtering process, so schedule the change appropriately if inbound mail is busy. In a multi-MTA setup, install and reload the rule on every MTA that may receive the test message. The Zimbra custom-rules example also uses zmamavisdctl restart after writing a local rule.

Send a controlled test message from an external account through the normal inbound route. Use a subject or body that should match the test rule, then inspect the original message headers or SpamAssassin report for the rule name and its score, commonly visible in X-Spam-Status. Compare the reported total to the configured spam threshold on your system. A matching rule adds to the score; it does not by itself guarantee that the message will be moved to Junk, since final handling depends on Zimbra’s Amavis and mailbox filtering configuration.

A dark terminal-style panel with rows of colored status-like indicators
After the service reload, inspect a controlled test message’s spam report for the custom test name.

Quick reference and rollback

  • Location, ZCS 8.5 and later: /opt/zimbra/data/spamassassin/localrules/your-rule.cf.
  • Syntax check: /opt/zimbra/common/bin/spamassassin --lint.
  • Reload: zmamavisdctl restart, run as zimbra on each relevant MTA host.
  • Verification: send a controlled test through the normal route and confirm the custom test name appears in the spam report.

If mail starts receiving an unexpected score or lint fails after a change, remove or restore only the custom file you added, run lint again, and restart Amavis to reload the previous rules. For ZCS 8.0 and earlier, consult the version-specific Zimbra guidance before proceeding; the later localrules path should not be assumed.

References: Zimbra: SpamAssassin custom rules; Zimbra: Anti-spam strategies and version-specific rule locations; Zimbra: ZCS 8.5 SpamAssassin directory layout; Apache SpamAssassin configuration reference.

Leave a Comment

How to