Sieve Examples

Note that many of these examples are trivial, or are better handled by making use of Fastmail's existing features, such as rules, subdomain addressing, folder name matching, or aliases.

They are nonetheless provided to serve as an example for building your own custom scripts.

We'd like to thank the Fastmail users who contributed these examples.

File all messages from a recipient into a folder

if address :is "From" "pal@mypals.org" {
  fileinto "INBOX.My Best Pal";
  stop;
}

File all messages from a domain into a folder

if address :domain :is "From" "mypals.org" {
  fileinto "INBOX.Pals";
  stop;
}

File all undefined addresses at a virtual domain into a folder

This recipe is useful when you have defined a catch-all address, you only have a handful of "legitimate" addresses within your virtual domain, but nonetheless you want to preserve everything that comes there - just in case.

if allof(
  address :domain :is "X-Delivered-To" "mydomain.info",
  not address :localpart :is "X-Delivered-To" ["address1", "address2", "address3"] # these are valid addresses
) {
  fileinto "INBOX.Possible spam";
  stop;
}

File messages to some aliases into alias-dependent folders.

We don't really read the mailing list that comes to alias2, hence, we'll be marking it as read right away. Redirect everything else to another account without keeping it in INBOX

if address :is "X-Delivered-To" "alias1@fastmail.fm" {
  fileinto "INBOX.alias1";
  stop;
} elsif address :is "X-Delivered-To" "alias2@sent.com" {
  setflag "\\Seen";
  fileinto "INBOX.alias2";
  stop;
} elsif address :is "X-Delivered-To" "alias3@eml.cc" {
  fileinto "INBOX.alias3";
  stop;
}
redirect "another@account.net";

Backup

Backup everything to a backup account. Keep the original messages at FM. redirect "backup@gee-mail.com"; keep;

Tiered spam filtering for SpamAssassin score

Three level spam filtering based on SpamAssassin rating.

  1. The first line discards anything with a score >= 20 (canned spam).
  2. The second line puts anything with 20 > score >= 8 (fairly spam) into a subfolder of spam called spam.
  3. The third line puts anything with a score < 8 (potential Ham) into a subfolder of INBOX called spam.

Note the addition to the require statement.

require ~[..., "comparator-i;ascii-numeric"];

if  header :value "ge" :comparator "i;ascii-numeric" ["X-Spam-score"] ["20"]  {
  discard;
  stop;
}
if  header :value "ge" :comparator "i;ascii-numeric" ["X-Spam-score"] ["8"]  {
  fileinto "INBOX.spam.spam";
  stop;
}
if  header :value "ge" :comparator "i;ascii-numeric" ["X-Spam-score"] ["4"]  {
  fileinto "INBOX.spam";
  stop;
}

Spam filtering based on Bayesian filter

Filter e-mail based on the SpamAssassin Bayesian filter rating.

if header :contains ["SPAM", "X-Spam-hits"] ["BAYES_40",
                                             "BAYES_44",
                                             "BAYES_50",
                                             "BAYES_56",
                                             "BAYES_60"]
{
  fileinto "INBOX.Junk Mail";
  stop;
}

Filtering known messages as sent

This filters messages addressed to sent@domain.com into "Sent Items" folder and mark them as "read". Useful if you want to save messages sent from an external client into your imap folder - just bcc all outgoing messages to sent@domain.com

require ["envelope", "fileinto", "reject", "vacation", "regex", "relational", "comparator-i;ascii-numeric", "imapflags"];

if header :is ["X-Delivered-to"] "sent@domain.com" {
  setflag "\\Seen";
  fileinto "INBOX.Sent Items";
}

Do not filter known senders

Avoid filtering any messages that are from "known senders" (your contacts) or from domains that you have decided are safe.

if not anyof(
  header :contains ["X-Spam-known-sender"] "yes",
  header :contains ["from"] ["amazon.co.uk", "myworkdomain.com"]
) {
  # ...filtering code goes here...
}

Time-sensitive notification example

Note: This example uses the 'date' header, so the times are local to the timezone set in your account.

This script notifies you on the app when someone emails your Fastmail account while you're at work. The script only works during a defined time period, so it doesn't send unnecessary notifications when you're not in the office.

The script: * Only applies this rule to emails from people you know * Uses regex to look at the time (HH:MM:SS) in the 'date' header * Looks to see if the hours are between 08 and 17 (disregard minutes and seconds) * Does not apply the rule if it's Saturday or Sunday * Does not apply the rule on some particular days (for holidays) * Does send notifications to the Fastmail app on your device * Includes 'from', 'subject' and a summary of the body text * Keeps the original email in my inbox

  elsif allof (
               header :contains "X-Spam-known-sender" "yes",
               header :regex "date" "(08|09|10|11|12|13|14|15|16|17):..:..",
               not header :regex "date" "(sat|sun)",
               not header :contains "date" [
                       "31 Mar 2006",
                       "03 Apr 2006",
                       "04 Apr 2006",
                       "07 Apr 2006"
       ] ) {
       addflag "$notify";
       keep;
       }