Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Email Filter Sieve: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To set up an email filter with Sieve, you need a mail server that supports the ManageSieve protocol (such as Dovecot, Cyrus, or a hosted provider that offers Sieve), a Sieve script written in the standard Sieve language, and a way to upload that script to the server. The shortest working path is: write a script with a require line, one or more if rules, and fileinto actions; upload it via ManageSieve (using a client like Thunderbird with the Sieve add-on, or the sieveshell command-line tool); then test by sending yourself a message that matches a rule.

What You'll Need

  • A mail account on a server that supports Sieve filtering (check your provider's documentation; many self-hosted Dovecot and Cyrus setups do, but not all hosted email services expose it).
  • A Sieve client: either a desktop email client with ManageSieve support (for example, Thunderbird with the Sieve add-on installed from the official add-ons site) or command-line access to sieveshell or managesieve if your server offers it.
  • Your mail server's ManageSieve hostname and port (commonly 4190, but confirm with your provider).
  • Your normal email username and password (or an app password if your provider requires one for external clients).
  • A plain text editor for writing the script (Notepad, TextEdit, VS Code, etc.).

Step-by-Step

  1. Confirm Sieve is available. Log in to your mail provider's control panel or check its help pages for "Sieve" or "ManageSieve". If you run your own server, check that the ManageSieve service is enabled in your Dovecot or Cyrus configuration. If the provider does not mention Sieve at all, it likely does not support it; you would need to use the provider's own webmail filters instead.
  2. Open your Sieve client. In Thunderbird, install the Sieve add-on from the official add-ons site, then open the add-on's settings and add a new server entry with your ManageSieve hostname, port, username, and password. In a terminal, run sieveshell --user=you@example.com --server=mail.example.com:4190 (replace with your details). Menu labels and connection dialogs vary by client and version, so look for "ManageSieve" or "Sieve" in the client's settings if the exact wording differs.
  3. Create a new script. In the client, choose the option to create a new Sieve script and give it a name such as main. Some servers require one script to be marked active; you will activate it after uploading.
  4. Write the require line first. Every Sieve script that uses extensions beyond the base language must declare them. For filing mail into folders, start with:
    require ["fileinto"];
    If you also want to copy messages, use require ["fileinto", "copy"];. If you want to reject messages with a custom message, add "reject".
  5. Add your first rule. A basic rule that files messages from a specific sender into a folder looks like this:
    if address :is "from" "boss@example.com" { fileinto "Work"; }
    The folder name must match an existing mailbox on the server, or the server may create it depending on its settings. Use the exact case and spelling of the folder.
  6. Add more rules as needed. You can combine conditions with allof or anyof. For example, to file messages from a domain that also contain a subject keyword:
    if allof (address :domain :is "from" "example.com", header :contains "subject" "invoice") { fileinto "Invoices"; }
    Rules are evaluated in order. Use stop; inside a block if you want to prevent later rules from also acting on the same message.
  7. Add a catch-all at the end if you want. Without a final rule, unmatched messages stay in the inbox. If you want everything else filed elsewhere, add an unconditional else block or a final if true { fileinto "INBOX"; }. Be careful: a misconfigured catch-all can hide new mail.
  8. Upload and activate the script. In your Sieve client, use the upload or save button, then set the script as active. In sieveshell, use put main to upload a local file and activate main to make it live. The exact command names are shown by typing help in the shell.
  9. Test with a real message. Send yourself a message that matches one of your rules from an external account, then check that it lands in the expected folder. Also send a message that should not match and confirm it stays in the inbox.
  10. Check the server logs if nothing happens. On a self-hosted server, look at the mail log (often /var/log/mail.log or the journal for the mail service) for Sieve errors. On a hosted provider, use any error display in the webmail or control panel, or contact support.

Common Mistakes

  • Forgetting the require line. A script that uses fileinto without require ["fileinto"]; will fail to compile.
  • Using a folder name that does not exist and that the server will not create. Check the exact mailbox name in your mail client first.
  • Putting stop; in the wrong place. A stop; at the top level ends processing for that message, so later rules never run.
  • Mixing up :is (exact match) and :contains (substring). Using :is with a partial address will not match.
  • Uploading the script but not activating it. Many servers keep multiple scripts, and only the active one runs.
  • Assuming Sieve can do anything a full mail client can. Sieve is a filtering language; it cannot send new mail or run arbitrary commands.

Tips

  • Keep a copy of your working script in a text file so you can restore it if an edit breaks something.
  • Test new rules with a dedicated test address before applying them to your main account.
  • Use comments (lines starting with #) to label each rule so future edits are easier.
  • If your provider offers a webmail filter interface, it may generate Sieve behind the scenes; editing the script directly can overwrite those filters. Check before mixing both.
  • For large rule sets, order rules from most specific to most general, and use stop; only when you are sure no later rule should apply.

Troubleshooting

If messages are not being filed, first confirm the script is active in your Sieve client. Then check the script for syntax errors by using the client's validate or compile option if it has one; otherwise re-upload and watch for an error message. If the upload succeeds but nothing changes, check that the rule's condition actually matches the test message (addresses can include display names, and address :is matches the bare address part). If you use a hosted provider, confirm that Sieve is enabled for your account and that you are editing the correct script. If the server rejects the script, the error usually names the line; common causes are a missing require or a misspelled extension. If you accidentally activate a broken script, deactivate it or upload a corrected version; most servers keep the previous active script until a new one is activated. On self-hosted servers, restarting the ManageSieve service after configuration changes may be needed, but check your server's documentation before restarting anything.

Frequently Asked Questions

Does Sieve work with any email provider?

No. It requires server-side support, usually through ManageSieve. Many self-hosted and some hosted providers offer it, but others only provide their own webmail filters. Check your provider's documentation.

Can I use Sieve to forward mail?

Yes, if the server supports the redirect extension. Add "redirect" to your require line and use redirect "you@example.com";. Some providers restrict forwarding, so confirm first.

What is the difference between Sieve and a webmail filter?

Sieve is a standard scripting language that runs on the mail server, so it works regardless of which email client you use. Webmail filters are usually provider-specific and may only apply when using that webmail.

Can I have multiple Sieve scripts?

Yes, most servers allow several scripts but only one active at a time. You can switch the active script in your Sieve client without deleting the others.

Is Sieve case-sensitive?

String comparisons are case-insensitive by default in Sieve unless you use the :case comparator with the comparator extension. Folder names may be case-sensitive depending on the server.

What happens if my Sieve script has an error?

The server will usually reject the upload and keep the previous active script running. If a broken script does become active, mail delivery may fall back to the inbox or be deferred; check the server logs and deactivate the script.