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" "" {
  fileinto "INBOX.My Best Pal";

File all messages from a domain into a folder

if address :domain :is "From" "" {
  fileinto "INBOX.Pals";

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" "",
  not address :localpart :is "X-Delivered-To" ["address1", "address2", "address3"] # these are valid addresses
) {
  fileinto "INBOX.Possible spam";

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" "" {
  fileinto "INBOX.alias1";
} elsif address :is "X-Delivered-To" "" {
  setflag "\\Seen";
  fileinto "INBOX.alias2";
} elsif address :is "X-Delivered-To" "" {
  fileinto "INBOX.alias3";
redirect "";


Backup everything to a backup account. Keep the original messages at FM. redirect ""; 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"]  {
if  header :value "ge" :comparator "i;ascii-numeric" ["X-Spam-score"] ["8"]  {
  fileinto "INBOX.spam.spam";
if  header :value "ge" :comparator "i;ascii-numeric" ["X-Spam-score"] ["4"]  {
  fileinto "INBOX.spam";

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",
  fileinto "INBOX.Junk Mail";

Filtering known messages as sent

This filters messages addressed to 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

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

if header :is ["X-Delivered-to"] "" {
  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"] ["", ""]
) {
  # ...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";