Skip to main content

Sieve Filters: Write and Test Server-Side Email Rules

Sieve filters sort incoming mail on the server, before any mail app sees it. This guide explains how a Sieve script is built, shows four short examples and describes how to upload and test a script without losing mail.

What is a Sieve filter?

Sieve is a language for filtering email at the time of final delivery, standardized in RFC 5228. The mail server runs your script for each newly delivered message. The rule therefore works while your computer is off. A script does not go back over messages that are already in the mailbox.

You can meet Sieve in several places:

Supported extensions differ between providers. Check your provider’s list before you copy a script from elsewhere.

How a Sieve script is structured

require ["fileinto"];
# One rule: a test, then actions in braces
if header :contains "subject" "invoice" {
fileinto "Invoices";
stop;
}

If no action cancels it, the server applies an implicit keep: a message that matches nothing stays in your inbox.

Four example Sieve scripts

These are examples, not rules we have tested on your mailbox. Replace the addresses, words and folder names with your own.

File mail from one sender

require ["fileinto"];
if address :is "from" "newsletter@example.com" {
fileinto "Newsletters";
stop;
}

File by words in the subject

require ["fileinto"];
if header :contains "subject" ["invoice", "receipt"] {
fileinto "Invoices";
stop;
}

A list of strings matches when any one of them is found. :contains looks for a substring, so invoice also matches “Invoices”.

File a mailing list by List-Id

require ["fileinto"];
if header :contains "list-id" "<project-announce.lists.example.org>" {
fileinto "Lists";
stop;
}

Mailing lists identify themselves in the List-Id header (RFC 2919). The header may carry a description in front of the identifier, which is why the example uses :contains and not :is. Copy the identifier from the headers of a real message.

A rule with an exception

require ["fileinto"];
# Everything from the shop, except invoices and order mail
if allof (address :domain :is "from" "shop.example.com",
not header :contains "subject" ["invoice", "order"]) {
fileinto "Offers";
stop;
}

Invoices and order mail from the shop stay in the inbox through the implicit keep.

To combine the examples, put one require line at the top and the rules below it in the order you want them checked. The destination folders should already exist: RFC 5228 leaves it to the server whether a missing folder is an error, is created, or sends the message to the inbox. How subfolders are written also depends on the server.

Upload a Sieve script

There are two common ways to get a script onto the server:

  1. A rule editor in webmail. You click conditions and actions together and the provider stores them as Sieve. Fastmail and Proton Mail also let you edit the code itself, as their linked help pages describe.
  2. A ManageSieve client. ManageSieve (RFC 5804) is the protocol for uploading and activating scripts; its registered port is 4190. Many webmail programs include a ManageSieve client, according to the Dovecot documentation. A user has at most one active script, so new rules belong in that script.

Pick one way per mailbox. mailbox.org, for example, advises against editing the same filters in its webmail and in an external tool.

Test a Sieve filter safely

  1. Check the syntax first. A ManageSieve server must reject a script that is not valid, and the protocol has a command that checks a script without storing it.
  2. Start with fileinto into a test folder. Add discard or redirect only after you have seen which messages the rule matches. A discarded message is gone without notice.
  3. Send yourself a matching message, a near miss and the exception case. Then look at where each one landed.
  4. Use a test tool if you have one. On a Dovecot server, sieve-test prints the actions a script would take for a stored message without executing them. Fastmail’s help page links a Sieve tester for the same purpose.
  5. Keep a copy of the previous script, and check rule order and stop when a message lands somewhere unexpected.

Where Sieve reaches its limits

Sieve matches exact conditions, not meaning.

SituationWhat a Sieve rule can do
One sender, one list or a fixed subject referenceMatch it exactly
One address sends both invoices and offersOnly separate them if a header or a word tells them apart
The wording changes from message to messageNeeds a growing list of words
“Anything that needs an answer this week”No header states that

Some servers add a body test (RFC 5173). It also compares strings; it does not understand the text.

Whenever an exact condition exists, a Sieve rule is the simpler and better choice. It runs at delivery, needs no extra service and behaves predictably.

Where AI sorting helps beside Sieve

AI Email Filter is a separate tool for the cases in the lower rows of the table. It does not generate or upload Sieve scripts. It connects to Gmail or to any reachable IMAP mailbox with supported credentials and works on messages after delivery, so your Sieve rules still run first.

You describe categories in plain language. For each message it selects one category or none, using the sender, the subject and a body preview. The action can be move, keep or delete, with optional effects such as forwarding with a summary. Test before you automate:

  1. Connect your IMAP mailbox; for a self-hosted server, see the Mailcow guide.
  2. Describe one narrow category and choose a destination folder.
  3. Test the category on real messages before you switch on automation.
  4. Review the action history; supported moves can be undone.

Avoid letting a Sieve rule and a category act on the same messages, or you will not know which one moved them. The first 200 classifications are free and need no credit card.

Next steps

If you sort in a desktop app instead, see Thunderbird filters. For provider details, read AI sorting for Fastmail, Posteo and mailbox.org.

Common questions about Sieve filters

Do I need AI Email Filter to use Sieve?

No. Sieve is part of your mail server or provider, and everything above works without us.

Does a Sieve filter sort mail that is already in the inbox?

Not by itself. Sieve runs when a message is delivered. Some providers offer a separate way to apply rules to existing mail; check their documentation.

Ready to put your inbox on autopilot?

Get Started Free