Even more sieve extensions

Technical

This is the twelfth post in the 2017 FastMail Advent Calendar. The previous post spoke about our Dropbox integration. In the next post, we talk about how to share the love with our Referral program.


What is Sieve?

Using rules to organise your mail as it arrives is a reasonably well understood concept. What is less well known is that the engine which runs the rules is called Sieve.

A sieve script consists of a number of tests which are applied to incoming mail: if a mail matches a test, then the actions associated with that test are performed.

Our rules system builds sieve scripts in the background.

test: "If the sender matches mom@example.com"
action: "File into folder `URGENT: Mom`"

You can see your sieve script by going to your Mail Settings → Rules screen and using the "Edit custom sieve code" link.

Please be aware: Sieve is powerful and does have the ability to permanently delete and alter messages with no recourse to backup or undo your actions. If you're not confident understanding Sieve and its extensions, using our rules and saved searches is safer.

What is a Sieve extension?

An extension provides extra mechanisms to test email or to take action on matching email. This capability has allowed the Sieve language to stay current over time, as additional headers and flags have become standard, and as people seek to use their email in more powerful ways.

While the list of possible Sieve extensions is enormous, FastMail periodically builds in support for additional extensions where we feel it benefits our users without compromising security.

Onto the good stuff: information on the new extensions you can now use!

New extensions

Variables extension

The filtering: variables extension lets you store and reference named data, allowing you to use information matched out of the test condition in the action body.

You can explicitly set variable values:

set "dollar" "$";

Variables can be filled using matching against patterns:

# Imagine the header
# Subject: [acme-users] [fwd] version 1.0 is out
if header :matches "Subject" "[*] *" {
    # ${1} will hold "acme-users",
    # ${2} will hold "[fwd] version 1.0 is out"
    fileinfo "INBOX.lists.${1}"; stop;
}

Caution: when match variables are used, strings can contain arbitrary values controlled by the sender of the mail.

Vacations seconds

The vacation seconds extension lets you specify the frequency your vacation auto-away message is sent down to the granularity of seconds. Use this in combination with the vacation extension for fine control of who receives what auto-away message, when.

Usage:

vacation [":days" number | ":seconds" number]
              [":subject" string]
              [":from" string]
              [":addresses" string-list]
              [":mime"]
              [":handle" string]
              <reason: string>

Example:

vacation :addresses ["friend1@example.edu", "friend2@.example.edu"]
        :seconds 1800
        "I am in a meeting, and do not have access to email.";

Imap4flags

Imap4flags supercedes the imapflags extension. It allows you to set different IMAP flags on message delivery.

Example (which also demonstrates the variables extension):

    if header :contains "from" "boss@frobnitzm.example.com" {
        setflag "flagvar" "\\Flagged";
        fileinto :flags "${flagvar}" "INBOX.From Boss";
    }

You can test for flags, add flags, and remove flags. Flag management can be combined with the variables extension, as above.

Editheader

The editheader extension allows you to add and delete header information from incoming mail.

Example:

   /* Don't redirect if we already redirected */
   if not header :contains "X-Sieve-Filtered"
           ["<kanyana@job.example.com>", "<kanyana@home.example.com>"]
   {
           addheader "X-Sieve-Filtered" "<kanyana@job.example.com>";
           redirect "kanyana@home.example.com";
   }

Note that changing header information can break DKIM signing if the signature includes header information.

Detecting duplicates

The detecting duplicates extension lets you test for duplicates on incoming mail. This is mostly useful when handling duplicate deliveries commonly caused by mailing list subscriptions or redirected mail addresses.

Example:

if duplicate {
     fileinto :create "Trash/Duplicate";
}

Further information

This has only been a taste of what each extension can do. The full list of options and possibilities is too large to explore in a single blog post.

Our help has more information on scripting with sieve, how to test sieve scripts, and a sieve FAQ.