Fun with Sieve rules

07.26.2018 - written by  in Domain names

9 Comments

Sieve lets you put more or less complex filter rules on the server side of your email mailboxes. Once the rule has been put in place, it will be applied to incoming mail, regardless of the mail client you’re using (Outlook, Thunderbird, Apple Mail, webmail, etc.).

Previously, Sieve rules were only available through SOGo, our webmail platform, but now they’re available from any email software.

With Thunderbird, for example, you can use the Sieve extension from Thomas Schmid that you can add via add-ons, but this type of add-on exists for just about any mail client you might have.

You can find documentation with examples on the wiki page for Dovecot, the IMAP server that Gandi Mail uses:

https://wiki2.dovecot.org/Pigeonhole/Sieve/Examples

Or on Wikipedia:

https://en.wikipedia.org/wiki/Sieve_%28mail_filtering_language%29#Example

Let’s take a look at how you could use Sieve filters from day to day.

Imagine you receive around 3,000 emails a day in your email inbox at work and about 200 in your personal email, both of which you access through your desktop as well as your phone. Here’s how Sieve filters could help you sort through this quantity of email efficiently on the server side of things:

  1. Distinguish between email addressed to you, and email from distribution lists

    Especially in a professional context, you might find you get two kinds of email: email addressed directly to you or that you’re copied on and email you receive by virtue of being on a distribution list. You can sort these so that only email directly addressed to you or that you’re copied on shows up in your Inbox, while email from distribution lists goes to a separate folder.

    First, remember that the line ‘require’ must be the first line in a Sieve file. If you want to use all of the following examples be sure to put them altogether in a single file that starts with ‘require’:

    require ["fileinto"];

    Then, if you’re the To: or CC: on an email:

    if header :contains ["to","cc"] "you@domain.tld" {
    fileinto "INBOX";
    stop;
    #Then say you want email from a @gandi.net email address in a separate folder
    } elsif header :matches "From" "*@gandi.net>" {
    fileinto "INBOX/Gandi";
    #Otherwise you can have everything go into a folder to sort through later
    } else {
    fileinto "INBOX/to-sort";
    }

  2. Use addresses yourname+whatever@ when you sign up at a new site, like yourname+amazone@domain.tld, etc.

    require ["variables", "envelope", "fileinto", "subaddress"];
    #Add the code from above

    if envelope :is :user "to" "yourname" {
    if envelope :matches :detail "to" "*" {
    #That way, everything that follows the + goes into a variable called 'name'
    set :lower :upperfirst "name" "${1}";
    }
    #If 'name' is empty, the email stays in the Inbox
    if string :is "${name}" "" {
    fileinto "Inbox";
    #Otherwise, it goes into the folder corresponding to the part after the +
    } else {
    fileinto "plus/${name}";
    }

  3. Delete spam

    Soon we’ll be introducing a new anti-spam update but already with Sieve, if an email has been flagged as SPAM, you can filter it to your Spam folder:

    if header :contains "X-Spam-Flag" "YES" {
    fileinto "Spam";
    }

    Then, if the Spam level is detected as high, defined by number of asterisks, you can reject the email directly, according to how sensitive you’d like your spam filter be

    if header :contains "X-Spam-Level" "*******" {
    discard;
    stop;
    }

  4. Manage your auto-reply

    When you leave on vacation (soon, we hope), you can add your away message directly in your Sieve file.
    “days 1” indicates that the away message only gets sent once per day per address

    require ["fileinto","imap4flags","regex","vacation"];
    # rule:[Auto-reply]
    vacation
    :days 1
    :subject "Out of the office until ..."
    text:
    Hello,
    I'm on vacation until ... and won't be checking my email regularly during this time.
    If this is an emergency, please contact ... at example@domain.tld.
    Sincerely,
    Your Name
    .
    ;

  5. Don’t miss messages from important people

    You can also flag messages from important people using Sieve, and set notifications on the mail client on your phone to notify you when you get a message that’s been flagged:

    require ["imap4flags"];
    if envelope "from" "important@person.tld" {
    setflag "\\Flagged";
    stop;
    }

These are just a few examples of rules that you can use with Sieve, but we encourage you to play around with Sieve. Have some fun with it and post any good rules you come up with in the comments!

Leave a Reply

    Simon

    Hi,

    Thanks for those handy examples. I’m not sure to understand the introduction though.

    When you say “but now they’re available from any email software” does this mean that we can create our own rules on your server to filter at the source?

    If so, how do we do it if our email client doesn’t provide any add-on for it (Spark for instance)?

    Do you provide any documentation on how to interact with your server to setup those rules?

    Thanks! <3

      To create the server side rule file, you will need a compatible software, you can use for example Thunderbird just for that and still use Spark.
      Any soft, if you use the IMAP protocole is just a way to display your mailbox.

    Joshua

    Great! I’ll use Gandimail.

    Adam Booth

    Looks as though theres a curly brace missing from the plus addressing rules; also, i’ve configured the plus addressing in thunderbird, and it doesn’t seem to be working – is there anything else I need to do? thanks. adam.

      Adam Booth

      Just to be clear, in addition to the missing curly brace, to make it work, you do need to edit example 2) to your own username, and as it is there, you need to create the “plus” folder, and any folders your want mail to go into, or it’ll go into a black hole; never to be seen again.

      If you change the line

      fileinto “plus/${name}”;

      to

      fileinto :create “plus/${name}”;

      It will create the folders itself on demand – you can also change “plus” to “Inbox” or something else of your choice, to put the subfolders somewhere else within your mail folders.

      Adam Booth

      The same applies for the Spam example – if you create a “Spam” folder manually, it’ll be fine – if its not there, the mail will all disappear. for safety, you can change the

      fileinto “Spam”;

      line to

      fileinto :create “Spam”;

      Sorry, I forgot that you also need to add the

      “mailbox” exteniosn to the “require” line too, for the :create keyword to work..,

    GFS

    I’m so pleased to have the Sieve filters *server side*!! It has enabled me to effectively deal with spam, that I’d been struggling with for years. Being able to simply ‘reject’ known spam is fantastic, so thank you for the effort. 🙂 🙂

    Question:
    Is it possible, using the SOgo UI, to create Rules using your examples? I can’t see a way to do this.

      GFS

      Hi Sophie,

      thanks for the reply.

      That post doesn’t actually answer what I’m looking for. Specifically, I’m interested in knowing if it’s possible to use the SOGo UI, to create Rules using code, as in the example posted above.

      For example … can I add this code, using the SOGo UI? (Not can I *emulate* it. Can I use actual written code?)

      if header :contains [“to”,”cc”] “you@domain.tld” {
      fileinto “INBOX”;
      stop;
      #Then say you want email from a @gandi.net email address in a separate folder
      } elsif header :matches “From” “*@gandi.net>” {
      fileinto “INBOX/Gandi”;
      #Otherwise you can have everything go into a folder to sort through later
      } else {
      fileinto “INBOX/to-sort”;
      }