DEV Community

Ahsan Luqman
Ahsan Luqman

Posted on

How to Filter Spam Emails with Custom Rules

By Ahsan

A custom email rule is a server-side predicate that a mail forwarding service evaluates against each inbound message before delivery. The rule pairs a set of conditions on the message with a set of actions, and the service applies the actions when the conditions match. The conditions read fields from the envelope, the headers, the attachments and the authentication results, and the actions reroute, transform, quarantine or drop the message. Rules of this shape run at the forwarding service, before the message reaches the destination inbox.

Evaluation order

Rules run in priority order, starting from priority 1, and each rule specifies a match type of ALL or ANY across one to five conditions. ALL requires every condition to hold, while ANY requires at least one. A rule carries one to five actions, and all of the actions run when the rule matches. Each rule also carries an optional stop_processing flag, and when a matched rule sets the flag, evaluation of lower-priority rules ends for that message. A broad rule placed above a narrow rule absorbs the messages the narrow rule was written to catch.

Authentication failure on a shopping alias

The first rule targets messages that fail authentication checks on an alias given to online stores. The rule has two conditions with match type ALL: the alias_address field is the shopping alias, and the spf_status field is fail. A message that satisfies both conditions is sent to quarantine, which encrypts the raw message with AES-256-GCM into a vault for seven days and halts delivery, and the message waits there for inspection, release or discard in the application. A quarantined message can be released if the rule matched legitimate mail, while a dropped message cannot be recovered. The rule uses quarantine rather than a silent drop because SPF failure also occurs on legitimate mail: forwarding breaks SPF when a message passes through an intermediary, so a failed check on its own does not establish that the message is unwanted. The alias condition narrows the scope, so the same failure on an alias given to a bank or a government service does not trigger the rule.

Invoice subject with an executable attachment

The second rule targets a pattern in which a message carries the word invoice in the subject and an executable file in the attachments. The rule has two conditions with match type ALL: the subject field contains "invoice" and the attachment_name field ends with ".exe". The action for this rule is a silent drop, which means no forwarding, no bounce and no notification to the sender. A legitimate invoice arrives as a PDF or as a link to a billing portal, while an executable attachment on an invoice-themed message has no function in the delivery of an invoice. The subject condition on its own would catch genuine invoices, and the attachment condition on its own would catch legitimate software installers, so the two conditions together define a narrower pattern than either one alone.

High spam score on a catch-all address

The third rule uses the numeric spam score together with the catch-all flag. The rule has two conditions with match type ALL: the spam_score field is greater than 10 and the is_catchall field is true. A message that satisfies both conditions is sent to quarantine. The spam score comes from Rspamd, and the Rspamd documentation sets the default action thresholds at 4 for greylisting, 6 for adding a spam header and 15 for rejection. A threshold of 10 therefore sits between the header threshold and the rejection threshold, catching messages the classifier scores as strongly suspicious without reaching the level at which Rspamd rejects outright. The catch-all condition narrows the rule to messages that arrived at an address the user never gave out.

Regular expressions in subject matching

The matches_regex operator applies a regular expression to a string field, with matching case-insensitive. A subject condition with the pattern "(urgent|immediate action|account suspended)" matches any subject containing one of the three phrases, which covers the urgency language that appears in credential phishing messages. The operator takes a single pattern, so one condition covers several phrases that would otherwise need one contains condition each. A regex condition on its own has a wider match than a contains condition on a single phrase, so rules that use it combine it with a second condition on authentication or sender domain before the rule takes a terminal action.

Limits of rule-based filtering

Rule-based filtering has limits that follow from what the engine can read. A rule reads the envelope, the headers, the attachment names and the authentication verdicts, and it does not read the decrypted body of an encrypted message or the content of a linked page. A sender domain that has never sent spam has no history for a rule to match, so the first message from a new spam domain passes every condition that depends on the sender. A rule that drops messages on a failed SPF check also drops legitimate mail forwarded through an intermediary, since forwarding breaks SPF for wanted and unwanted mail alike. The engine evaluates each message once, at the moment of arrival, and it does not revisit a message when later information about the sender becomes available. AliasFleet provides conditional routing rules through its Rules API, with the same condition-action shape described above, and the Free tier allows no rules.

Top comments (0)