1. Home
  2. Email
  3. Sieve
  4. Sieve Syntax and Common Recipes

Sieve Syntax and Common Recipes

Learn about sieve syntax and how recipes are set up. This article is not a comprehensive list of everything that sieve can do, but rather a guide to helping you get started with sieve. If you want more information about what sieve is, see our article What is Sieve Filtering?

Syntax

Below is the basic format of a sieve script. Depending on your chosen email client you may need to know the syntax in order to write your own sieve rules.

Sieve uses if statements to test the email. If the condition following the if is not met, then the email will move on to the next statement or, if there are no more statements, go to the inbox.

if <condition> {
<action>; 
<action>; 
...
}

Sieve also uses else if statements so that you can test multiple conditions in a succession if the first statement is not met.

if <condition> {
<action>; 
<action>; 
...
}

else if <condition> {
<action>;
...
}

else {
<action>;
}

The <condition> specifies the part of the email that should be tested and what it should be tested for.

The <action> indicates what to do with the email if it meets the requirements. For example, you might reject or redirect the email.

For more information about the different types of actions, conditions, and more sieve syntax, check out this great resource by Thomas Schmid.

Common Recipes

If you want to use sieve to help filter your email, you can use these basic recipes to help you get started.

Automatically Delete Mail from a Specific Address

If you want to automatically delete mail from an address, you can do so. Here is an example of what the command may look like:

if address :is “From” “bad@example.com” {
discard;
stop;
}

By using address and “From”, the command will search incoming email’s From address to see if it matches the contents of the second quotations. In this case, the contents is bad@example.com. If the From address matches bad@example.com, then the discard action will run. The discard action immediately deletes the email, keeping it from landing in your inbox.

Automatically Reject Mail Over a Certain Size

If you want to automatically reject an email that exceeds a certain size, the command will resemble this:

require [“reject”];

if size :over 1024K {
reject “Your message was not delivered. The message’s size was over the limit accepted by the recipient.”;
stop;
}

The command example above checks incoming messages to see if they are over 1024 kilobytes. If they are, they execute the action below, which rejects the message and sends the sender a “Your message was not delivered. The message’s size was over the limit accepted by the recipient” message.

Reject does not work in combination with other actions, except for discard, capture, vacation, and hold.

Redirecting Mail Based on Subject Line

If you want to redirect mail based on words in the subject line, the command will look like the following:

if header :contains “subject” “help” {

redirect “support@example.com”;

stop;

}

By using header in the conditional statement, the command checks the entire email header. By using “subject,” we narrow down the results to only the subject line. After that, we establish the word we are looking for in the subject line: “help.”

If the conditions of the rule are met, then the action will be executed. In this case, if the subject line contains the word “help”, then the email will be redirected to support@example.com.

If you want to search for multiple words in the subject line, you can do so by adding brackets around the words that could be found in the subject line.

if header :contains “subject” [ “help”, “assistance”, “problem”] {
redirect “support@example.com”;
stop;
}

If any of the words listed within the brackets are found in the subject line, the email will be redirected. Only one word needs to be found in order for the command to execute.

Combining More than One Condition

If you want to filter an email based on multiple conditions, you can. By using all of, you can list multiple conditions that the email must meet before the action is run.

if allof ( address :is “From” “customer@example.com”, header :contains “subject” “order” ) {
fileinto “Sales Folder”;
stop;
}

The example command above will check incoming mail to see if it is from the address customer@example.com. If it is, it will also see if it contains the word “order” in the subject line. If both these conditions are met, the email will be sent to the “Sales Folder.”

If none of the recipes above fit your needs, you can check the Tutorials and Examples section of the sieve.info site. You can also see a PDF of sample sieve scripts by Vircom.

Updated on July 11, 2018

Was this article helpful?

Related Articles