Basic Sieve Usage Examples

Note

Unlike conventional message filters that you may be used to using on your email applications, you don’t make a lot of little filters with Sieve that you activate/deactivate one-by-one. Rather, you will be putting all of your filters (each one is referred to as a command block) into a single filter file, that in turn must be activated. If you need to change or remove any of the filters, you need to edit the file containing the block.

require command

At the top of your filter file, you must include the list of Sieve features (separated by commas) that you need to use in order for your code blocks (separate filters) to use.

For example, the following line will let you make use of the features in parentheses in your command blocks:

require [“fileinto”, “vacation”];

You will put your filter command blocks directly under this line.

NOTE: We currently support the following extensions:

fileinto reject regex mailbox foreverypart
envelope imap4flags date extracttex
encoded-character copy index  
vacation subaddress include ihave  
comparator-i;ascii-numeric variables duplicate  
relational body mime  

commenting

If you want to make it easy for others (or yourself, later) to remember what your command blocks are for, you can leave comments. There are two ways to do this:

Starting a line with a hash

Anything after the hash symbol (#) will not be taken into account. This can be useful for not only comments, but temporarily deactivating a filter, by putting a # at the beginning of each line in your command block, like this:

#if address :is ["from", "sender"] "support-renew@gandi.net"
#
#{
#        fileinto "Inbox/Renew";
#        stop;
#}

using a C-style comment

Another way to leave a comment, one that you want to have in a paragraph format, can be done like this:

/* This starts with a forward slash
 * and then each line that follows
 * must start with a "*" symbol and
 * end like this */

Sample filters

There are much more advanced features than those described here, and you can find information on them from the various links provided at the end of this page. The goal here is to just provide the new Sieve filter user with a basic understanding of how they are used.

Filing emails into a folder

Let’s say that I want to have a filter puts all my Gandi Domain renew reminders in a special subfolder of my Inbox that I call, “Renew”. The Sieve filter block for this would look like the following:

if address :is ["from", "sender"] "support-renew@gandi.net"

{
        fileinto "Inbox/Renew";
        stop;
}

The name of the folder you use is relative to the root location. So “Inbox/Renew” is for a subfolder of Inbox called Renew. If you just put, for example “Inbox” it would go to your main Inbox folder.

Tip

the optional “stop;” command that you see in the above example tells the filter to stop if the mail meets that condition. It will therefore not continue to execute any command blocks after that one.

Setting a simple out-of-office reply

Here you can see a way to set a out-of-office reply (it will overwrite any message you put in your out-of-office reply message field on our website):

if allof(currentdate :value "ge" "date" "2018-08-01",
         currentdate :value "le" "date" "2018-08-15")
{
    vacation
    :days 1
    :subject "Gone fishing"
    text:

  "Hello,

   I have gone fishing and will return sometime next week.

   Mr. Smith
.
;
}

The top two lines in the above example demonstrate an optional way to include a start and stop date for the out-of-office reply. If you want to use this, you would need to include the “date” and “relational” extensions, in addition to the “vacation” extension.

Use mr.smith+anything@ whenever I need it

It is frequently useful to create a specific email address for a website, when they ask you to provide an email address.

You can use your Gandimail email address plus “+” anything to create an alias. For example mr.smith+amazon@ would then get all my emails from amazon. What is useful here, is that you can have Sieve automatically create the folder to separate mails sent to that address to its own folder.

require ["variables", "envelope", "fileinto", "subaddress"];

if envelope :is :user "to" "mr.smith" {
 if envelope :matches :detail "to" "*" {
   /* Save name in ${name} in all lowercase except for the first letter.
    * Anything, anything, ANYTHING thus all become 'Anything'.
    */
   set :lower :upperfirst "name" "${1}";
 }

 if string :is "${name}" "" {
   /* Default case if no detail is specified */
   fileinto "Inbox";
 } else {
   /* For mr.smith+anything@ this will become plus/anything */
   fileinto "plus/${name}";
 }
}

a full sieve filter file example

Here is what a complete Sieve filter file looks like, combining the filters above.

It is a good practice to comment your code to help you or others better see what you did. These are added to demonstrate this as well.

#this is a comment to say that below are the extensions I need for my command blocks to work.

require ["variables", "envelope", "fileinto", "subaddress", "date", "vacation", "relational"];


#this is a comment to say that below is the automatic filing into a folder

if address :is ["from", "sender"] "support-renew@gandi.net"

{
        fileinto "Inbox/Renew";
        stop;
}


#this is a comment to say that below is the out-of-office reply

if allof(currentdate :value "ge" "date" "2018-08-01",
        currentdate :value "le" "date" "2018-08-15")
{
    vacation
    :days 1
    :subject "Gone fishing"
    text:

 "Hello,

  I have gone fishing and will return sometime next week.

  Mr. Smith
.
;
}


#Below is to create and file into a subdirectory, addresses I make on the fly.

if envelope :is :user "to" "mr.smith" {
 if envelope :matches :detail "to" "*" {
   set :lower :upperfirst "name" "${1}";
 }

 if string :is "${name}" "" {
   fileinto "Inbox";
 } else {
   fileinto "plus/${name}";
 }
}

So when an email arrives in this mailbox, it will put any renew reminders from Gandi into my “Renew” subfolder, and send an out-of-office reply. If I go to a website called examplecompany and make an account on it with mr.smith+examplecompany@ any emails they send to me at that address will do to a new folder that Sieve will automatically make called “examplecompany”.