Sieve Filter Rules

Last modified by Aysegül Omus on 2024/04/05 12:56

1. Syntax

1.1. Compare

Sieve contains a total of 6 comparisons, including 3 string comparisons (text) and 3 integer comparisons (numbers).

String comparisons:

  • :is means that the content of the compared field must be exactly equal to the specified value.
  • :contains causes the condition to be met if the keyword is included in the compared field.
  • :matches means that the compared field must match the specified wildcard expression, i.e with '?' (exactly one character) and '*' (0 to infinitely many characters). To get a normal '?' and '*' you need to write '\\?' and '\\*'.

Integer comparisons:

  • :over means that the value of the field must be above the specified value.
  • :under means that the value of the field must be less than the specified value.
  • :count counts the number of values ​​in the specified field.

In addition, it is still possible to check whether a specific field exists in the header:

  • :exists checks whether a field is set in the header and is particularly useful in combination with the "not" operator e.g. to test if a subject is set.

1.2. Control structures and blocks

To make case distinctions Sieve uses the usual if, else, elsif structures:

  • :if means that the actions specified in the following block will be executed if the specified condition is met.
  • :else can be used after an "if" structure and the actions specified in the following block are executed if the condition of the preceding if structure is not met.
  • :elsif combines together an "else" with an "if" structure and can be understood like an "if" structure inside an "else" block.
  • :allof ( ... ) represents a combination of several conditions and is satisfied when all "subconditions" are met (AND operation). These are given as a comma-separated list in brackets.
  • :anyof(...) represents a combination of several conditions and is fulfilled if at least one \"sub-condition\" is fulfilled (OR operation). These are given as a comma-separated list in brackets.
  • :not... negates the following condition.
  • :[ … ] serves to list elements, e.g. a string list: ["Max", "Martin", "Manuel"].
  • :{ … }serves to delimit an action block.

1.3. Fields

The following fields are available for comparisons:

  • :header This field is probably the most diverse, since it allows access to all header fields . It requires no "require" statement.
  • :address This field gives direct access to all address fields of the header. It supports at least the From, To, Cc, Bcc, Sender, Resent-From, Resent-To fields and implementation-dependent ones. Does not require a "require" statement.
  • :size This field allows the comparison of the size of the email. Requires no "require" statement.

1.4. Actions

 stop; is used to end the execution of the script. Does not require a "require" statement.

  • reject "... ";  causes the email to be sent back to the sender. Between the quotation marks a message can be entered, which the initial sender gets, e.g. the reason for the refusal. Requires  no "require" statement, but  recommended.
  • discard; causes the email to be irrevocably deleted without informing the sender. Requires no "require" statement, but  recommended.
  • keep; causes the email to be saved in the default folder. Overrides any preceding discard action. Requires no "require" statement.
  • fileinto "... "; causes the email to be saved in the specified folder. The folder name is entered between the quotation marks. If the folder does not exist, the message is stored in the default folder and no further action is taken. Requires a "require" statement.
  • redirect "... "; causes the email  to be redirected . The destination address is in quotation marks. Requires  no "require" statement, but  recommended.

1.5. Diverses

One-line comments are started with a '#'. Multi-line comments are started with "/* " and ended with " */".

Some functions have to be explicitly included at the beginning of the script. This is possible with a require statement.

Example:

require ["fileinto", "reject"];

2. Examples:

2.1. Simple examples

The following example sorts all emails from one of the 3 specified addresses into the "friends" folder:

if address :is :all "From" ["bob@example.com", "alice@example2.com", "bff@somewhere.de"] {
   fileinto "INBOX.friends";
}

This example shows the use of an "if" structure, the "address" field, an exact comparison using ": is" and a list in "[...]", as well as the "fileinto" action.

It is also possible to search for keywords in the subject line:

if header :contains "subject" "Facebook" {
   discard;
}

As a result of this rule, all emails whose subject contains the keyword "Facebook" are simply deleted because they are definitely spam.

The following example rejects emails larger than 1MB:

if size :over 1M {
   reject "Please do not send me emails with large attachments, since my email storage is limited";
}

2.2. Move spam emails to a separate folder

if header :contains "X-Spam-Level" "*******" {
     # Wenn der Spam als gelesen markiert werden soll:
     # addflag "\\Seen";

    fileinto "INBOX.Junk";
}

2.3. Ignore certain emails

if address "From" "virusalert@informatik.tu-muenchen.de" {
    discard;
}

2.4. Forward or move by subject

if header :contains "subject" "my_project" {
    # Zum Verschieben in einen Ordner
    # fileinto "INBOX.project";

    # Zum Weiterleiten mit lokaler Kopie
   redirect :copy "me@example.com";

    # Zum Weiterleiten ohne lokale Kopie
    # redirect "me@example.com";
}

For more information about redirects, please read here.

2.5. Sort by Address Additions

require ["mailbox"];

if envelope :matches "to" "*+abc@*" {
    # Creates the respective folder auotmatically, if it does not exist yet and moves the email, which contains the squence *+abc@*.
   fileinto :create "INBOX.abc";
}