Skip to content
xaweho

Knowledge base · advanced

Setting Sieve filters per mailbox as domain admin

Prefilters and postfilters for individual mailboxes of your domain — for example to sort newsletters into a specific folder or to reject certain senders.

advanced ·
For:Domain adminAdmin

Sieve is a filtering language for mail servers — standardized, powerful, and built right into Mailcow. As domain admin you can set Sieve filters for individual mailboxes of your domain: as a prefilter before or as a postfilter after the user’s personal filters.

There’s no filter that applies only to the mailboxes of one domain. As domain admin you set filters per mailbox: every filter belongs to exactly one mailbox. If a rule should apply to several mailboxes, you create it for each mailbox separately. Server-wide there are only the admin’s “Global Prefilter” and “Global Postfilter”.

If mail from a specific newsletter sender should count as “wanted” across the whole domain and not end up in spam, you don’t need a Sieve filter. That’s what the domain’s allowlist is for: E-Mail → Configuration → Domains tab → Edit → Spam filter tab.

Important: personal filters that only one user wants for themselves belong in their personal Sieve in webmail. Admin filters are for rules you want to enforce for a mailbox.

Creating a filter

In the Mailcow GUI:

  1. E-Mail → Configuration → Filters tab.
  2. Click Add filter.

In the dialog:

Field Value
Username the mailbox the filter applies to
Filter type “Prefilter” or “Postfilter”
Short description descriptive text
Script see below
Active enabled

Click Validate to check the syntax, then Add. Per mailbox and filter type, only one filter is active — the dialog warns you that activating this script deactivates all other filters of the same type. So multiple rules for one mailbox go into one shared script.

Prefilter vs. postfilter

  • A prefilter runs before the user’s personal filter kicks in. Used e.g. to discard or redirect mail before the user filter sees it.
  • A postfilter runs after the personal filter has finished. Used e.g. as a last safety layer to sort mail if the user has no own filter for it.

Rule of thumb: postfilters complement the user’s filters. You use prefilters when you want to enforce a rule.

The Sieve language — the most important patterns

Sieve is not a scripting language. You describe conditions and actions, no program flow.

Pattern 1: sorting a newsletter sender into a folder

require ["fileinto", "envelope"];

if anyof (
  address :is "from" "newsletter@firma-x.com",
  address :contains "from" "@info-newsletter.de"
) {
  fileinto "Newsletter";
  stop;
}

Mail from these senders automatically lands in the “Newsletter” folder — in the mailbox you set the filter for.

Careful: the target folder has to exist in that mailbox. If it isn’t there, the mail lands in the INBOX.

Pattern 2: discarding certain senders entirely

require ["envelope"];

if address :is "from" "spam@bekannter-spam.com" {
  discard;
  stop;
}

Mail from that sender is discarded outright, never arrives in that mailbox, and the sender gets no bounce.

Careful: discard is final — no way to get the mail back later. For important senders, prefer fileinto "Spam"; over discard;.

Pattern 3: flagging mail with certain keywords

require ["fileinto", "imap4flags"];

if header :contains "subject" "[Rechnung]" {
  fileinto "Rechnungen";
  addflag "\\Flagged";
  stop;
}

Mail with “[Rechnung]” in the subject is sorted into the “Rechnungen” folder and marked with a flag (star).

Pattern 4: redirecting mail to role addresses

require ["envelope"];

if address :is "to" "alt-info@deinverein.de" {
  redirect "info@deinverein.de";
  stop;
}

Mail to the old address is forwarded to the new one. Similar to an alias — but conditional when needed, with date filters or other conditions.

Pattern 5: rejecting mail with certain content

require ["body", "reject"];

if anyof (
  body :raw :contains "0xDEADBEEF"
) {
  reject "Diese Mail wurde aus Sicherheitsgründen abgelehnt.";
  stop;
}

Mail with suspicious content is rejected with a bounce message.

The Sieve language — key directives

Directive What it does
require ["..."] Enables required Sieve extensions. Always goes at the very top.
if, elsif, else Conditional logic.
anyof, allof, not Boolean combination of multiple conditions.
address, header, envelope, body What is matched against.
:is, :contains, :matches, :regex How it is matched.
fileinto "folder" Move into a subfolder.
discard Discard.
redirect "..." Forward.
reject "reason" Reject with a bounce.
addflag, setflag Mark with IMAP flags (Flagged, Seen, Answered).
stop Abort the filter, stop processing the mail.

Full reference: RFC 5228.

Testing filters

Once you’ve created a new filter:

  1. Save the filter with the Active box unticked at first.
  2. Send a test mail to the mailbox the filter applies to — one the filter should catch.
  3. Enable the filter. Careful: this switches off any filter of the same type that’s already active for that mailbox.
  4. Test again.

For syntax errors, Mailcow already refuses to save — you get an error message with the line number.

For logic errors (the filter catches the wrong mail), you have to think it through yourself — or open a ticket, we’ll help.

Best practices

Document your filters. Put a descriptive text in every filter’s “Short description” field. “Filter 1” says nothing. “Newsletters from info-newsletter.de into folder X” is clear.

Use prefilters sparingly. Prefilters override user decisions — users can’t do anything about it unless you change it. If a user later wants a different sorting and your prefilter stands in the way, they’ll be annoyed. Prefer postfilters that complement user filters.

Be careful with spam patterns. Spam senders change domains and addresses constantly. Sieve-based spam filters often go stale quickly. Better: tune Mailcow’s own spam filter (Rspamd) at the server level — ask us.

Never discard for unknown senders. If you want to discard a sender entirely, be sure nothing important could be among it. Otherwise, prefer fileinto "Verdacht";.

Frequently asked questions

Does a filter automatically apply to new mailboxes too? No. Every filter belongs to exactly one mailbox. For a new mailbox, you create the filter there again.

Can users see the filters you set for their mailbox? Not in SOGo — there they only see their personal filters. If a user wonders why a mail landed in a certain folder and can’t find a matching filter of their own, it’s probably a filter you set for their mailbox.

How can I test a filter before going live? Mailcow has no built-in Sieve tester. Best practice: save the Sieve in the filter edit (with Active unticked), then send a test mail the filter would catch to the mailbox it applies to (try it on a test mailbox first) — and check what happens. Step by step. Careful: activating it switches off any active filter of the same type for that mailbox.

Can I have multiple filters in parallel? Per mailbox, only one filter per filter type is active — so at most one prefilter and one postfilter. If you activate another one, Mailcow switches off the others of that type. So multiple rules go into the same script. The order is fixed: global prefilter → prefilter → the user’s filters → postfilter → global postfilter.

What happens when a filter has an error? Mailcow logs the error in the server log. As domain admin you have no access to the logs — open a ticket. The mail is still delivered — that’s the intended “fail open” behavior, so a broken filter doesn’t paralyze the whole mailbox.

Can I set Sieve filters per mailbox separately? Yes, that’s exactly how the “Filters” dialog works: every filter belongs to one mailbox. The user maintains their own personal filters in SOGo webmail. More in Personal Sieve filters in webmail.

How do I debug a filter that doesn’t kick in? Add an extra fileinto "Sieve-Debug"; to the Sieve script — a folder where you can see which mails passed through the filter. If the debug folder stays empty, the condition doesn’t match.

What’s next

Next steps

Tags
mailcow sieve filter domain-admin

Did this article help?

If not, open a ticket. If it did, we're happy about a referral — both sides get €25 credit on their customer account.