Sieve Filters: Taming a Noisy Inbox on the Server


Hello, tech enthusiasts! 👋

You open your inbox on Monday morning and see 140 new emails. Most of them are system notifications: “Your report is ready”, “New login”, “Weekly summary”. Somewhere in the middle are the three emails from real people that actually need you.

Rules in a desktop email app only run when that app is open, so your phone still shows all 140 emails. There is a better way: Sieve.

What you need

  • An email account on a server that supports Sieve (many do, especially servers that use Dovecot)
  • Access to your webmail, for example Roundcube or SOGo, or a server admin who can add a script for you

Step 1: Understand what Sieve is

Sieve is a small language for email rules. It runs on the mail server, at the moment a message arrives. Think of it like a mail room worker who sorts the letters before they go into your box. Your laptop can be off, and the sorting still happens.

Because the sorting happens on the server, the folders look the same everywhere: on your laptop, on your phone, and in webmail. 📱

Step 2: Find where to add your rules

In most webmail apps, look in Settings, then Filters (or “Rules”). Many have a simple form and an option to edit the filter set as raw text. We will write raw text, because you can copy it.

If your company runs its own mail server, the admin can also add a script for your mailbox.

Step 3: The first lines

Every Sieve script starts by saying which extra features it needs. This is the require line.

  • fileinto lets you move a message into a folder.
  • imap4flags lets you set flags, like “read”.
  • mailbox lets the script create a folder if it does not exist yet.
require ["fileinto", "imap4flags", "mailbox"];

Step 4: Write the rules

Rules run from top to bottom. The command stop means “this message is done, do not check more rules”. Here is a full example. Change the addresses to your own.

require ["fileinto", "imap4flags", "mailbox"];

# Rule 1: mail from my own team always stays in the inbox
if address :domain :is "from" "example.com" {
    keep;
    stop;
}

# Rule 2: automatic notifications go to a folder, already marked as read
if address :localpart :is "from" ["noreply", "no-reply", "notifications"] {
    fileinto :create "Notifications";
    addflag "\\Seen";
    stop;
}

# Rule 3: daily reports go to their own folder (unread, so you notice them)
if header :contains "subject" ["Daily report", "Weekly summary"] {
    fileinto :create "Reports";
    stop;
}

# Rule 4: mailing lists go to a folder
if exists "list-id" {
    fileinto :create "Lists";
    stop;
}

# Rule 5: mail not sent to me directly (for example, to a group address)
if not address :is ["to", "cc"] "me@example.com" {
    fileinto :create "Group Mail";
    stop;
}

# Everything else stays in the inbox
keep;

Two kinds of tests are used:

  • address tests look at an email address in a header. You can check the whole address (:is), only the part after the @ (:domain), or only the part before it (:localpart).
  • header tests look at any header as text. :contains means “has this text somewhere inside”.

Notice "\\Seen". The flag is really \Seen, but in Sieve text the backslash must be written twice.

Rule 5 is the “keep mail addressed to you” rule. If your address is in To or Cc, the message stays. If you got it through a group address, it moves away.

Step 5: Test safely

A wrong rule can hide important emails. So be careful:

  1. Never use discard or reject while testing. Always use fileinto. A message in a folder can be found again. A discarded message is gone.
  2. Add one rule at a time. Send yourself a test email after each one and check where it lands.
  3. Check the folders daily in the first week.

On the server, Dovecot includes sieve-test. It shows what a script would do with a saved email, without delivering anything:

sieve-test my-rules.sieve test-message.eml

Common problems

  • The script is rejected with an error about :create. Your server does not support the mailbox extension. Remove "mailbox" and :create, and create the folders by hand first.
  • Mail goes to a strange folder like “INBOX.Reports”. Some servers need the full folder path. Check how your folders are named in webmail and use the same name.
  • A rule never matches. Many systems send notifications from addresses like alerts@mail.example.net. Open the email, view the original headers, and copy the exact address.
  • Nothing happens at all. Make sure the filter set is active. Many webmail apps let you keep several sets but only one is active.

Wrap-up

With five rules, your inbox shows only the emails from people who need you. Notifications wait quietly in their folder, on every device.

Next idea: add a rule that flags emails with words like “urgent” or “invoice” from your important contacts, using addflag "\\Flagged", so they stand out with a star.