New guide: how to warm up a dedicated IP without hurting your reputation. Read now →
Log in
MigoSMTP
Getting startedUpdated 26 Oct 2026

Sieve Mail Filtering: Server-Side Rules for Sorting, Forwarding and Vacation Replies

What Sieve is, how scripts run on the mail server, common rules for folders, forwarding, spam handling and vacation replies, extensions, testing with managesieve, pitfalls with forwarding and authentication, and security notes.

MO
Written by Mateus Oliveira
Senior SMTP and MTA Engineer
Reviewed by Hannah Mitchell
  • Published20 Feb 2025
  • Read time6 min
  • Length1,260 words
Share this guide

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.

WhereOn the mail server, during final delivery, before the message reaches the inbox
LanguageA small declarative language: tests, actions and "require" for extensions
ManagementEdit through the webmail filter page or upload with the ManageSieve protocol
CareForwarding and auto-replies interact with SPF, DKIM and loop prevention

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

ExtensionWhat it addsExample use
fileintoDeliver into a named folderMove newsletters out of the inbox
envelopeTest the SMTP envelope (MAIL FROM, RCPT TO)Match the real recipient behind a catch-all or alias
variablesStore and reuse textBuild folder names from parts of an address
subaddressTest the +detail part of user+detail@domainSort mail by plus-address tag
imap4flagsSet IMAP flagsMark messages as read or flagged
vacationAutomatic replies with rate limitingOut-of-office message
copyRedirect or fileinto while still keeping a copyForward and keep
regex (non-standard in some servers)Regular expression matchingComplex pattern rules
spamtest, virustestCompare score levels from the server's filtersSort by spam score
includeCompose scripts from partsShared team rules plus personal rules

Useful rules

GoalRule idea
Sort a mailing listif header :contains "List-Id" "name" { fileinto "Lists"; stop; }
Sort by plus-addressUse the subaddress extension: if address :detail :is "to" "invoices" { fileinto "Invoices"; }
Handle spam headersTest the server's X-Spam-Flag or X-Spam-Status header and file into Junk. See spam scores
Keep a copy while forwardingredirect :copy "[email protected]";
Drop messages from a blocked senderdiscard; silently, or reject "No longer accepted"; (see the warning below)
Prioritise VIP sendersaddflag "\\Flagged"; then keep
Large messagesif size :over 20M { fileinto "Large"; }
Shared mailbox rulesMatch 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 redirect resends 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 reject sends 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

  1. Webmail. Roundcube, SnappyMail, Fastmail and others offer a visual filter editor that writes Sieve for you; use the raw editor for advanced rules.
  2. ManageSieve. This protocol (default port 4190) lets clients upload, list and activate scripts. Tools such as sieve-connect and sieve-test help automate it.
  3. Validation. sievec script.sieve (Dovecot) checks syntax; sieve-test script.sieve message.eml shows what the script would do to a sample message.
  4. Order and stop. Rules run in order, so put specific rules before general ones and use stop to prevent later rules from acting twice.
  5. Folders must exist, or use the :create option of fileinto if supported; otherwise the message falls back to the inbox.
  6. Logging. Enable the server's Sieve log or the debugging option to see which rule fired. See reading mail logs.
  7. 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.

Explore SMTP relay

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.

Topics#sieve#filtering#dovecot#rules#mailbox
Was this guide helpful?Your answer helps us improve our documentation.

Start sending in minutes.

Create an account, add your domain and send your first email.

Get started Talk to sales