Mail clients offer rules for sorting messages into folders, but those rules run only while the client is open and differ from program to program. Sieve is a standard language (RFC 5228) for filtering mail on the server at the moment of delivery. A Sieve script lives with the mailbox, runs for every incoming message whatever device you use and is understood by Dovecot, Cyrus, Stalwart, Fastmail and many hosting panels. It is small, safe by design (no loops, no arbitrary programs) and powerful enough for the filtering that most people and teams need. This guide explains how scripts work, shows practical rules for folders, spam, lists and vacation replies, covers forwarding pitfalls and testing and notes security aspects. It builds on the List-Id header, auto-replies and forwarding.
Structure of a script
require ["fileinto", "envelope", "variables", "vacation", "imap4flags"];
# 1. Mailing lists into their own folder
if header :contains "List-Id" "newsletter.mail.example.com" {
fileinto "Newsletters";
stop;
}
# 2. Mark spam and file it
if header :contains "X-Spam-Flag" "YES" {
fileinto "Junk";
stop;
}
# 3. Messages from a person or team
if address :is "from" "[email protected]" {
addflag "\\Flagged";
fileinto "Ops";
}
# 4. Everything else stays in INBOX (implicit keep)A script has three kinds of building blocks. Tests look at the message: header, address, envelope, size, exists, allof, anyof and not. Actions decide what to do: keep, fileinto, redirect, discard, reject and stop. Comparators and match types such as :is, :contains and :matches (with * and ? wildcards) control how text is compared. Anything beyond the core needs an explicit require line at the top. Scripts run top to bottom, and if no action discards the message it is implicitly kept in the inbox.
Common extensions
| Extension | What it adds | Example use |
|---|---|---|
| fileinto | Deliver into a named folder | Move newsletters out of the inbox |
| envelope | Test the SMTP envelope (MAIL FROM, RCPT TO) | Match the real recipient behind a catch-all or alias |
| variables | Store and reuse text | Build folder names from parts of an address |
| subaddress | Test the +detail part of user+detail@domain | Sort mail by plus-address tag |
| imap4flags | Set IMAP flags | Mark messages as read or flagged |
| vacation | Automatic replies with rate limiting | Out-of-office message |
| copy | Redirect or fileinto while still keeping a copy | Forward and keep |
| regex (non-standard in some servers) | Regular expression matching | Complex pattern rules |
| spamtest, virustest | Compare score levels from the server's filters | Sort by spam score |
| include | Compose scripts from parts | Shared team rules plus personal rules |
Useful rules
| Goal | Rule idea |
|---|---|
| Sort a mailing list | if header :contains "List-Id" "name" { fileinto "Lists"; stop; } |
| Sort by plus-address | Use the subaddress extension: if address :detail :is "to" "invoices" { fileinto "Invoices"; } |
| Handle spam headers | Test the server's X-Spam-Flag or X-Spam-Status header and file into Junk. See spam scores |
| Keep a copy while forwarding | redirect :copy "[email protected]"; |
| Drop messages from a blocked sender | discard; silently, or reject "No longer accepted"; (see the warning below) |
| Prioritise VIP senders | addflag "\\Flagged"; then keep |
| Large messages | if size :over 20M { fileinto "Large"; } |
| Shared mailbox rules | Match on the envelope recipient to separate several addresses delivered to one mailbox |
Vacation replies done properly
require ["vacation"];
vacation
:days 7
:subject "Out of office"
:addresses ["[email protected]", "[email protected]"]
"I am away until Monday and will reply when I return.";The vacation action replies at most once per sender in the period given by :days, and it should not answer mailing lists, bulk mail or automatic messages. A good implementation checks the Auto-Submitted, Precedence and List-* headers and never replies to the null sender. List all of the recipient's addresses with :addresses so the reply is sent only when the message was actually addressed to you. See the null sender and out-of-office templates.
Forwarding pitfalls
- SPF breaks. A
redirectresends the message from your server, so the next hop sees your IP, which is not in the original sender's SPF record. The forward may fail SPF at the destination, and DMARC with it, unless DKIM survives. See forwarding and authentication. - Envelope sender. Good servers rewrite the envelope sender when forwarding (SRS) so bounces return to the forwarder and SPF can be assessed for the forwarding domain.
- Loops. Two mailboxes that forward to each other multiply mail until a hop limit stops it. Test forwards before enabling them and prefer a single direction.
- Spam forwarding. Forwarding unfiltered spam damages your server's reputation at the destination. File spam before the redirect rule, or apply the redirect only to messages that passed the filter.
- Reject versus discard. A
rejectsends a bounce to the claimed sender, which for forged spam means backscatter. Prefer discard, or reject at SMTP time in the server configuration instead of in a user script.
Managing and testing scripts
- Webmail. Roundcube, SnappyMail, Fastmail and others offer a visual filter editor that writes Sieve for you; use the raw editor for advanced rules.
- ManageSieve. This protocol (default port 4190) lets clients upload, list and activate scripts. Tools such as
sieve-connectandsieve-testhelp automate it. - Validation.
sievec script.sieve(Dovecot) checks syntax;sieve-test script.sieve message.emlshows what the script would do to a sample message. - Order and stop. Rules run in order, so put specific rules before general ones and use
stopto prevent later rules from acting twice. - Folders must exist, or use the
:createoption of fileinto if supported; otherwise the message falls back to the inbox. - Logging. Enable the server's Sieve log or the debugging option to see which rule fired. See reading mail logs.
- Keep a backup of the active script in version control or in your documentation.
Organising rules for a team
Personal scripts are simple; shared mailboxes need structure. For a support or sales address that several people read, start with a short list of agreed folders, then write the rules that deliver into them and publish the list so that everyone knows what the folders mean. Use the envelope test to separate mail sent to different aliases that land in one mailbox, and use the include extension, where available, to keep a shared base script that administrators control and a personal script that individuals can edit. Name rules with comments that explain why they exist, not only what they do, for instance "newsletter from vendor X, review weekly". Revisit the set every quarter: stale rules for departed vendors, retired products or former colleagues cause surprises, and a catch-all discard rule that was added for a temporary problem can hide important messages for years. Finally, keep the most important rule last and simplest: whatever the other rules do, genuine mail from customers must always reach a folder that someone reads.
Security and governance
- Sieve cannot run programs, but it can redirect mail anywhere, so attackers who gain access to a mailbox often add a hidden forwarding rule. Review scripts after any compromise and alert on changes to filters.
- Restrict ManageSieve to authenticated, TLS-protected connections and apply the same lockout and rate-limit protection as for login. See brute-force defence.
- Limit external redirects by policy where data-loss controls apply, for example by blocking redirects to personal domains for staff mailboxes.
- Document shared team scripts, their owners and review dates.
Sieve gives you the sorting power of a mail client on the server, available on every device and robust even when your laptop is closed. Keep scripts short, test them against sample messages and treat forwarding with care, and your mailbox stays tidy without surprises.
Run your own mail flow with confidence Authenticated sending and clear logs.
References and further reading
Frequently asked questions
What is Sieve?
A standard language for filtering email on the server during delivery, defined in RFC 5228.
How do I upload a Sieve script?
Use the webmail filter editor or a ManageSieve client, then activate the script.
Can Sieve forward email?
Yes, with the redirect action, but forwarded mail may fail SPF and DMARC at the destination unless the server rewrites the sender.
Why prefer discard over reject?
Reject sends a bounce to the claimed sender, which for forged mail causes backscatter.
How do I test a script?
Validate with sievec and run sieve-test against sample messages before activating it.