How to Filter or Block Emails Based on Subject in SpamAssassin

July 15, 2025 /

SpamAssassin is a popular open-source spam filter that helps reduce unwanted emails by scanning for specific patterns, especially repeated keywords in the subject line. In this guide, you will learn how to create custom rules in SpamAssassin to automatically detect and block such emails, keeping your inbox clean and secure.

Follow the article:

  1. Locate SpamAssassin’s local configuration file, where you can set custom rules, such as which subject line emails to block or how much spam score to assign to certain messages. This is usually one of the following:
    /etc/spamassassin/local.cf

    or

    /etc/mail/spamassassin/local.cf
  2. Use a text editor like “nano” or “vi”:
    sudo nano /etc/spamassassin/local.cf
  3. Once the editor is opened, you can create a specific rule or instruction in SpamAssassin that checks the subject lines of incoming emails or compares them with certain repeated keywords or patterns, such as “Buy Now” or “Lowest prices”.
    header SUBJECT_LOWEST_PRICES Subject =~ /lowest prices/i
    score       SUBJECT_LOWEST_PRICES 10.0
    describe    SUBJECT_LOWEST_PRICES Block emails with subject “Lowest prices”

    Explanation:

    1.  header defines a rule named SUBJECT_LOWEST_PRICES that checks the Subject header.
    2. =~ /buy now/i is a regular expression match for “lowest prices”, case-insensitive.
    3. score assigns a spam score of 10.0 (enough to classify it as spam).
    4. describe provides a human-readable explanation of the rule.Y
  4. You can add multiple subject-based rules in the same file, for example:
    header      SUBJECT_BUY_NOW Subject =~ /buy now/i
    score       SUBJECT_ BUY _ NOW 10.0
    describe    SUBJECT_ BUY _ NOW Block emails with the subject “buy now”
  5. After saving your changes, “restart” SpamAssassin to apply the new rules.
    For systemd-based systems:

    sudo systemctl restart spamassassin

    Or, if SpamAssassin is combined with a mail server like Postfix, restart the mail service:

    sudo systemctl restart postfix
  6. To verify that the rule works, send a test email with the specified subject. If any of the above phrases are found in the subject line, SpamAssassin will flag the email as spam. Depending on your mail server’s configuration, the email may be moved to the spam folder, deleted automatically, or redirected to another mailbox.

Tips:

  1. Be careful when setting high scores, as false positives may happen.
  2. Frequently update and check your rules to avoid blocking genuine emails.
  3. Use regular expressions (/pattern/i) sensibly for comprehensive or more specific matching.

This way, SpamAssassin helps you in filtering unwanted emails. Customs rules provide flexibility, allowing you to customise your spam filtering to meet your specific requirements.

Our support team is available 24/7 to assist you. Feel free to reach out during any difficult time.

Looking to strengthen your spam protection? Learn How to enable outgoing spam filtering using SpamAssassin

Spread the love