Skip to content

Create server-side email filters (Sieve rules) in cPanel

How to build account-level and user-level email filters in cPanel to sort, redirect, discard, or stop unwanted mail server-side, and the order rules are applied in.

Server-side email filters act on mail as it is delivered, before any client downloads it — so a rule that files newsletters into a folder, forwards billing mail to accounting, or discards an abusive sender works the same on every device and even when no client is connected. cPanel exposes these as Email Filters, which it compiles to Sieve rules that Dovecot runs at delivery time. This runbook covers building both account-wide and per-mailbox filters, the conditions and actions available, and the order rules apply in so they do not fight each other.

What’s happening

A Sieve filter is a small rule: a set of conditions (the From contains, the Subject matches, the message is over a size) and one or more actions (deliver to a folder, redirect, discard, fail with a reason, stop). cPanel offers two scopes. Global Email Filters (WHM-level or the cPanel “Global Email Filters” interface) apply to every address on the account/domain; per-account Email Filters (cPanel » Email » Email Filters » Manage Filters for one mailbox) apply only to that mailbox. Both compile to Sieve and run inside Dovecot as each message is delivered, which is why they work without a client and across all devices.

Order matters. Filters run top to bottom, and an action like discard or stop halts further processing — so a broad “discard anything from this domain” rule placed above a “file mail from this person into a folder” rule will throw the person’s mail away before the folder rule ever sees it. Global filters generally run before per-account ones. Understanding this evaluation order is the difference between a filter set that does what you intend and one that silently drops wanted mail. cPanel also provides a filter test box so you can paste a sample message and see which rules would fire before trusting them with live mail.

How to verify

See the compiled Sieve rules for a mailbox and confirm Dovecot uses them.

# Per-user Sieve script cPanel generates from the filters
ls -la /home/<cpaneluser>/etc/example.com/user/ 2>/dev/null
cat /home/<cpaneluser>/.dovecot.sieve 2>/dev/null
find /home/<cpaneluser> -name '*.sieve' 2>/dev/null

# Account-level (global) filter store
cat /home/<cpaneluser>/etc/example.com/filter.yaml 2>/dev/null

Watch a filter act during delivery:

# Sieve filing/redirect/discard shows in delivery logs
tail -f /var/log/maillog | grep -i 'sieve\|lda\|lmtp'
grep -i 'sieve' /var/log/exim_mainlog | tail -20

Confirm a redirect or discard actually happened for a test message:

# Did a filtered message land in the expected folder?
ls -la /home/<cpaneluser>/mail/example.com/user/.Newsletters/cur/ | tail

The fix

  1. Choose account-wide or per-mailbox scope — use cPanel » Email » Global Email Filters for a rule that should apply to every address on the domain (e.g. block a spamming domain everywhere), and Email Filters » Manage Filters on a specific account for rules that affect only that mailbox (e.g. one user’s folder sorting). Pick the narrowest scope that does the job.

  2. Build the condition from the right header — in the filter editor, choose the part to match: From, To, Subject, Any Header, Body, or message size, and the operator (contains, is, matches regex, begins with). For tag-based or list-based sorting, match the To/Delivered-To header, which is more reliable than Subject.

  3. Pick an action deliberately — common actions are Deliver to folder (sort), Redirect to email (forward a copy), Discard (silently drop, no bounce), Fail with message (reject so the sender gets an NDR), and Stop processing (skip remaining rules). Use Discard for known spam (no backscatter) and Fail only when you want the sender informed.

  4. Order rules so broad blocks do not eat wanted mail — drag rules so specific “keep/file this” rules sit above broad “discard/stop” rules. Because a stop or discard halts processing, anything you want to rescue must be matched first. Review the list top to bottom and read it as the order of execution.

  5. Test before trusting it with live mail — use the filter test box in the Email Filters interface: paste a representative raw message and run it to see which rules fire and what action results. This catches an over-broad condition before it silently discards real mail.

  6. Avoid forward loops and accidental mass-redirects — a redirect filter that sends to an address which itself filters back can loop; and a global redirect on a busy domain can flood the target. Confirm the redirect destination is correct and not another filtered address that points back.

  7. Prefer filtering to deletion for spam you are unsure about — file questionable mail into a Junk/Review folder rather than discarding, so the user can recover false positives. Reserve hard discard for senders you are certain about. For domain-wide spam scoring, the per-account SpamAssassin filter complements these rules rather than replacing them.

Once a filter is in place and tested, mail is sorted or blocked the instant it is delivered, consistently across every client, with no per-device setup. The one thing to get right is rule order — a filter set that reads top to bottom the way you intend will behave the way you intend. Stack Harbor builds and audits cPanel mail filtering under cPanel/WHM management — start a conversation.