
Using Sieve Mail Filtering Language is a powerful way to manage your email inbox. You can use it to automate email processing tasks, such as moving messages to specific folders or deleting unwanted emails.
To get started, you'll need to write Sieve scripts, which are made up of commands that specify what actions to take on incoming emails. For example, a script might contain a "redirect" command, which forwards emails from a specific sender to a different address.
One of the key features of Sieve is its ability to handle multiple conditions and actions. This is achieved through the use of "if" and "elsif" statements, which allow you to specify different actions based on the characteristics of the incoming email.
Recommended read: Can I Use Bcc to Send Mass Email
What is Sieve?
Sieve is a programming language used to filter emails.
You can create filters in Sieve by writing simple rules, like giving all messages from Kyle the green label.
Sieve rules can be combined to create a sophisticated filtering system.
You can write Sieve rules from scratch, or use software that makes it easier.
The interactive interface is a great way to create Sieve filters, and it uses a subset of Sieve.
Sieve is more powerful than just reordering your mailbox, it also allows you to reject emails with a response message.
Getting Started
To get started with Sieve, you need to understand its syntax, which is quite simple.
The Sieve language is made up of a list of commands, and in most scripts, it starts with a require command. This command loads an extension that provides a certain functionality.
To load multiple extensions, you can use a list. This is useful when you need to perform different actions on an incoming email.
You can load extensions like imap4flags, which allows you to flag mail as read. After loading the required extensions, you can perform tests on the incoming message.
These tests are done by combining if and another command, such as address or header.
Check this out: Aol Mail Slow to Load
If the tests succeed, you can apply an action to a message. For example, you can put all emails from a specific sender into the same folder and flag the incoming mail as read.
You can pass a list of strings to the address command, which will try to find a match by trying any of the strings in the list.
Here's a list of some of the Sieve extensions you can use:
- imap4flags: loads an extension that allows you to flag mail as read
- fileinto: allows you to put a message into a folder
- addflag: allows you to flag a message as read
Remember, whenever you can pass a string to a test condition, it's usually also possible to pass a list. This can be useful when you want to apply the same action to multiple emails.
Language and Syntax
Sieve is a data-driven programming language, similar to earlier email filtering languages such as procmail and maildrop, and earlier line-oriented languages such as sed and AWK, which specifies conditions to match and actions to take on matching.
The language has limitations, including no variables and no loops, which confine it to simple filtering operations. However, it does allow conditional branching, preventing runaway programs.
The language also has a restricted grammar to reduce the complexity of parsing, but it supports multiple methods for comparing localized strings and is fully Unicode-aware.
Here are some key language constructs:
- [ ... ]
- { ... }
- if
- elsif
- else
- stop
How Does Work?

Sieve works by using simple rules written in the Sieve scripting language. These rules are applied at the mail server level, filtering messages before they're seen by any mail software.
Sieve rules can be created from scratch, borrowed from examples, or made easier with software. They need to be put in a text file and placed on the server.
The rules are applied at the mail server level, not just within individual mail programs. This means Sieve rules can filter mail across all devices, not just the one you're using.
Sieve rules can test if a message header matches a specific key, perform logical OR operations on tests, or add flags to messages being processed.
Here are some specific actions and tests that Sieve supports:
Language
Sieve is a data-driven programming language, similar to earlier email filtering languages such as procmail and maildrop, and earlier line-oriented languages such as sed and AWK.
It specifies conditions to match and actions to take on matching, differing from general-purpose programming languages.
Suggestion: Is Html a Programming Language
Sieve has many limitations, including no variables and no loops, which generally confine the language to simple filtering operations.
Extensions have been devised to extend the language to include variables and limited loops, but the language is still highly restricted.
Sieve supports the use of multiple methods for comparing localized strings and is fully Unicode-aware.
Here's a list of some of the building blocks of Sieve:
- [ ... ]
- { ... }
- if
- elsif
- else
- stop
A comparator is something you use to compare a field with a certain value, and it always starts with a colon, the ':' character.
Here are some examples of comparators:
- :is
- :matches
- :contains
- :over
- :under
- :count
Sieve filters don't have access to the actual content and only show the encrypted size.
Regular Expression Support
Regular Expression Support is limited in Proton's implementation of Sieve, following the Sieve email filtering standard. This standard prioritizes simplicity and compatibility across platforms.
Some common regular expression shorthand syntax, such as \b (word boundary), \w (word character), \W (non-word character), and \d (digit), are not supported in Sieve. As a result, using these shorthand characters in filters may cause them to fail silently.
The limitation is due to the Sieve standard itself, not a specific implementation choice. For full details, see the Sieve Regular Expressions Draft RFC.
Here are some examples of unsupported regular expression shorthand syntax:
- \b (word boundary)
- \w (word character)
- \W (non-word character)
- \d (digit)
Writing a Script
A basic Sieve script is a simple text file that contains a series of commands to filter your email. This script can be automatically generated by Fastmail.
It's worth noting that the automatically generated Sieve script from Fastmail might look slightly different from a basic example script.
A basic Sieve script demonstrates the use of actions, if, elsif, and else tools, which are fundamental components of the Sieve language.
Write a Script
To write a script, you'll need to include the necessary "require" lines at the top. This is because many commands in Sieve require specific require commands to function properly.
You can include every possibility at once, which can make large scripts a bit less efficient, but it's unlikely you'll notice a difference.
Start by defining the actions you want to take, such as filing a message into a folder. For example, you can file a message to "[email protected]" into a folder named "Personal".
Use if, elsif, and else tools to create conditional statements in your script. This can help you make decisions based on specific criteria.
A basic example of a Sieve script might look like a simple list of commands, including actions and conditional statements.
You might like: How to Find Personal Email Addresses
Modifying a Message's Subject

Modifying a message's subject can be tricky, as it can make it impossible for other software to verify DKIM signatures.
You can modify a message's subject using the deleteheader and addheader commands, which are described on superuser.com.
Be aware that modifying a subject can cause issues with DKIM signatures, but if you're aware of the problem and want to do it anyway, you can use these commands.
Here's an example that appends "[SPAM]" to the subject of a message by using these commands.
Explore further: Html Email Subject
Testing and Validation
You can test your Sieve script by saving your changes and sending yourself emails to see how the various clauses work. This can be inefficient, especially if you want to test matches against external senders or generated headers.
Use the Sieve testing tool to test your script. Simply paste your full Sieve script and a raw message, and you can modify the headers to suit your needs.
Here's an interesting read: Email Headers Examples

The address command is useful for testing headers, such as the from, to, and sender header. You can extract different parts of the email address using flags like :localpart, :domain, and :all.
Here are some examples of how to use the address command:
You can use these flags to perform tests on email addresses, such as checking if the local part is equal to a certain value.
The Sieve testing tool provides a range of tests, including size, header, address, and more. You can use these tests to validate your Sieve script and ensure it's working as intended.
Some examples of tests include:
- Testing if a message has a certain flag
- Testing if a message has a certain header or value
- Testing if a message size is over or under a certain limit
These tests can be used to validate your Sieve script and ensure it's working correctly.
Actions and Operations
Actions in Sieve can be used to perform various operations on messages, such as adding flags, moving messages to folders, and discarding messages.
You can use the "fileinto" action to move a message to a specific folder. For example, "fileinto :mailbox{/home/user/mailbox}" will move the message to the specified mailbox.
Broaden your view: Android Forward Text Messages to Email

The "imap4flags" package provides several actions, including "setflag" which adds a specified flag to the message being processed, and "removeflag" which removes a specified flag.
The "variables" package allows you to create new variables and associate them with values. For instance, "set :name 'John'" will set a variable named "name" to the value "John".
Some actions can be used to discard or reject messages, such as the "discard" action which discards the message at the end of the filter, and the "reject" action which rejects the message.
Other actions can be used to send emails or auto-respond to senders. For example, the "send" action sends an email to the SMTP address of the sender with a specified reason, and the "vacation" action sends an auto-response to the sender with a specified reason.
Here are some of the actions and their descriptions:
The "include" action stops processing the current Sieve filter but allows subsequent filters to run. The "stop" action stops processing all Sieve filters, preventing any subsequent filters from running.
Additional reading: Filters for Video Calls
Filtering and Matching
A standard syntax for mail filtering is essential to provide sorting and disposition mechanisms for email, especially with increasing mail traffic due to unsolicited email and mailing lists.
Many filtering schemes are in place, but they use variant underlying syntaxes, making it difficult for users and system administrators to create filters that interoperate with each other.
A standard syntax would allow software authors and vendors to write tools that use common scripts, making it easier to manage mail filtering.
To see what Sieve rules match against certain messages, you can use the "sieve-filter" test program from the command line shell.
You can also use a program named “sieve-test” to examine mail files outside your normal folders and get details about which rules match.
Here are some examples of how to use the “:domain” test to check only the part after the @ sign in an email address:
- File a message from “[email protected]” into a folder named “Someone”
- File messages from any address @example.org into the Trash folder
- Check whether the full “From” header address “contains” @example.org
You can also use the “address” command to perform tests on address headers, such as the from, to, and sender header, and extract different parts of the email address using flags like :localpart, :domain, and :all.
Block Specific Files

You can block specific files in your email by using Sieve filtering, which lets you examine the filenames and MIME types of attachments. This is useful if you want to avoid getting emails with certain file types.
For example, you can block messages with ".exe" or ".bin" file attachments by using the Sieve syntax to look for these file types in the Content-Disposition header of each MIME part.
This kind of filtering is more specific than general malicious filetype blocking, which can only catch top-level filenames and can't "look inside" zipped files. Be aware that this kind of filtering is complex and requires some technical knowledge.
It's worth noting that this kind of filtering is useful for avoiding unwanted emails, but it's not foolproof and may not catch all instances of unwanted files.
Take a look at this: Set up Html Mail Using Word
Filter by Message Size
Filtering by message size can be a great way to manage your inbox, especially if you're running low on space. You can use the Sieve filter language to flag large emails, making it easier to delete them.

To specify big sizes in Sieve, you can add a unit letter after the number. There are three units available: K for kilobytes, M for megabytes, and G for gigabytes.
For example, if you want to flag emails larger than 1GB, you would use "1G". This way, you can easily identify and manage large emails.
Discover more: Making Folders and Filing Important Emails
Filter by Domain Name
You can create filters that apply to all addresses at your domain name by placing script files named “domain-before.sieve” and/or “domain-after.sieve” in your domain’s home directory. This allows you to create domain-wide rules that override or supplement mailbox-specific filters.
These script files can be uploaded using a File Manager, FTP connection, SFTP, or a shell connection. The “domain-before.sieve” script runs before any per-mailbox “mailbox.sieve” script, and the “domain-after.sieve” script runs after.
The order of these scripts matters, as each script is used only if a previous script didn’t discard the message or file it into a folder. If you want to create domain-wide rules that override “mailbox.sieve” scripts, you’d put them in the “domain-before.sieve” script. To create domain-wide rules that are used only if “mailbox.sieve” doesn’t do anything, you’d put them in “domain-after.sieve”.
For your interest: Compressing a Video File for Email

Here are some examples of how you can use domain-wide filters:
You can use the “:domain” test to check only the part after the @ sign, as shown in the example: “file a message from “[email protected]” into a folder named “Someone””. This allows you to filter messages based on the domain name, rather than the specific address.
For another approach, see: Find Someone by Email for Free Gmail
Filtering Mailboxes
Filtering mailboxes can be a lifesaver for managing your email. A standard syntax for mail filtering is essential to simplify the process.
There are many filtering schemes in place, but most of them don't interoperate with each other, forcing users to recreate filters for each piece of software. This is why a standard syntax is necessary.
To use Sieve filtering for individual mailboxes, you need to place the Sieve script file in the mailbox directory. You can do this using a File Manager or FTP connection to your home directory.
Sieve filtering can also be applied to an entire domain name by placing script files named "domain-before.sieve" and/or "domain-after.sieve" in your domain's home directory. These scripts run before and after any per-mailbox "mailbox.sieve" script.
Additional reading: Email Filtering
The order in which these scripts run matters, as each script is used only if a previous script didn't discard the message or file it into a folder. If you want to create domain-wide rules that override "mailbox.sieve" scripts, you'd put them in the "domain-before.sieve" script.
You can discard all new mail by creating a sieve file with a single line: "redirect "trash";". However, be careful not to do this, as there's no way to get back discarded mail.
Sieve filtering is incredibly powerful, and here are some example rules to show what can be done: you can file a message to "[email protected]" into a folder named "Personal" by using a simple Sieve script.
Readers also liked: How to Create Html File for Email Signature
Matching Rules for Messages
Matching rules for messages is a crucial part of filtering and matching in Sieve. You can use the "header :contains" command to filter messages based on specific words or phrases in the message's headers.
Curious to learn more? Check out: Sending Text Messages from Email
For example, you can use "header :contains "From": to filter messages from a specific name, regardless of the address. This is particularly useful for avoiding treating messages as spam from trusted senders.
Sieve filters don't have access to the actual content of messages, only showing the encrypted size. This means you can use the "size" command to filter messages based on their size, such as flagging large emails to delete and save mailbox space.
You can also use the "comparator-i;ascii-numeric" command to provide number comparison, which is useful for filtering messages based on specific numbers or values.
Here are some examples of how to match rules for messages:
- File a message from "[email protected]" into a folder named "Someone": "fileinto :create "Someone" :contains "From" "[email protected]""
- File messages from any address @example.org into the Trash folder: "fileinto :create "Trash" :domain "example.org""
- Check whether the full "From" header address contains @example.org: "if any :all ( :header :contains "From" "@example.org" ) { ... }"
These examples illustrate how to match rules for messages based on specific criteria. By using the right commands and syntax, you can create effective filters to manage your email inbox.
Security and Limitations
Sieve has its limitations, and it's essential to know what they are before implementing it in your email system.

One of the limitations is that sent messages can't be moved manually or by filter to the Inbox or Drafts. They will always stay in the Sent folder and any folder/labels applied by the user or filter.
Another limitation is that received messages can't be moved manually or by filter to the Draft or Sent folders. This means you'll need to handle these messages differently than you would with regular emails.
Here are some key limitations to keep in mind:
Mailbombing Signups
Mailbombing signups can be a real nuisance, and it's not uncommon for people to get bombarded with messages asking them to confirm subscriptions.
Occasionally, we hear from people who are the victim of "mailbombing", where people try to sign you up for many lists. This can be a difficult situation to deal with.
The TT_NEW_SENDING_DOMAIN SpamAssassin rule matches messages from senders that our servers have never seen before, which can filter out some of these unwanted messages.
Limitations

As you set up your email system, it's essential to be aware of the limitations that come with it. You can't manually move sent messages to the Inbox or Drafts folder, and filters won't be able to do it for you either.
Received messages also can't be moved to the Draft or Sent folder by either manual action or filter. This is something to keep in mind when organizing your emails.
Drafts are similarly restricted and can't be moved to the Inbox or Sent folder. If you need to reuse a draft, you'll need to manually copy and paste its content.
If you're concerned about auto-responder or vacation messages, you'll be disappointed to know that they're not available for messages in the Spam folder. However, you can create a custom folder, like "mySpam", and disable notifications for it if needed.
Here are the key limitations to keep in mind:
- Sent messages can’t be moved manually or by filter to Inbox/Draft.
- Received Messages can’t be moved manually or by filter to Draft/Sent.
- Drafts can’t be moved manually or by filter to Inbox/Sent.
- For messages in Spam — or messages filed into spam in processing — we don’t send auto-responder/vacation messages.
Technical Details
Sieve rules can be complicated, and there's a lot to remember. You can specify a mail subfolder by listing all of the folders on the “path” to the subfolder, each separated by a dot.
For example, if you have a folder named “Travel” which then has a subfolder named “Summer Vacation”, you can specify the subfolder using "INBOX.Travel.Summer Vacation".
There are four different comparisons available: :is, :contains, :matches, and :regex. If you don’t specify a comparison, :is will be used.
The comparisons are used to match specific criteria in your email headers. For instance, you can use :is to match an exact, full match, or :contains to match a substring.
String comparisons are case-insensitive by default. Checking address "From" "[email protected]" does the same thing as checking address "From" "[email protected]".
If you really want case-sensitive comparisons, you can look up the details in RFC 3028 section 2.7.3.
The Sieve body check presents the message body as a single line string that contains no carriage returns or line feeds. This allows you to easily match regex rules against the entire message.
Here are the different comparisons available at a glance:
- :is (an exact, full match)
- :contains (matches a substring)
- :matches (a full match that allows DOS-style wildcards ? and *)
- :regex (which matches regular expressions)
Packages and Support

Sieve has a range of packages that can be used to extend its functionality.
One notable package is the "vnd.proton.expire" package, which includes a test for whether a message has an expiration time set.
The "vnd.proton.eval" package is another useful addition, allowing you to evaluate simple arithmetical functions given in a string.
You can also use the "vnd.proton.eval" package to transform variables.
The "extlists" package supports testing whether all given external lists are supported and valid.
Here's a brief overview of some of the packages available in Sieve:
Packages like "envelope" define tests around the SMTP envelope of the message, while "regex" provides the regex match operators.
The "envelope" package is useful for defining tests around the SMTP envelope of the message.
The "regex" package is a must-have for anyone working with regular expressions in Sieve.
The "vnd.proton.eval" package is used to evaluate simple arithmetical functions given in a string.
You might like: Anti-Spam SMTP Proxy
Featured Images: pexels.com


