Using Sieve scripts in Fastmail

Sieve is a programming language for filtering incoming emails. Sieve can be difficult to use, and is generally suitable for users with a high technical ability. Fastmail's flexible rules system provides most users with filtering functionality, but advanced users may choose to write custom Sieve scripts for complicated filtering (such as time-based rules).

What is Sieve?

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

Sieve scripts can be used to automatically delete or forward messages; to send autoreplies; to sort emails into folders as they arrive; to mark messages as read or flagged; to test messages for spam or viruses; or to reject messages at or after delivery. 

Setting up Sieve

To create custom Sieve scripts, go to the Settings → Mail rules screen and click Edit custom Sieve code at the bottom of the screen.

The rules system creates a Sieve script behind the scenes, and the advanced rule editing functionality allows you to write additional filtering actions to supplement your existing rules.

Sieve is a powerful language. It is possible to create a rule which permanently discards mail without the protection of our backup system, so be careful.

If you're not familiar with Sieve, we have a basic Sieve guide, including details on how to test your Sieve script.

You can also import or export your Sieve scripts as .json files by going to Settings → Mail rules and clicking Import or Export.

Modifying the Sieve script

The Sieve editing screen has four unchangeable blocks with Sieve code automatically generated by us based on choices you make within the Settings screens, separated by four editable text areas in which you can place your own custom Sieve code.

Sieve code is run in order, so custom code can be placed in different sections depending on which rules you want to be run first. In most cases, you will want spam filtering to be run on your messages, so you would usually place your code under the block that is generated for spam filtering.

In some limited cases, you might want your code to take place before spam filtering, in which case you would place your code in the top section, above the Sieve code generated for spam protection.

If you want to add a note to yourself so you remember why you added a piece of code later on, you can do this by adding # to the beginning of your line.

Sieve supported extensions

Sieve has a lot of extensions. Fastmail supports some of these, as given below:

This is encompassed by the line which appears at the top of your custom Sieve script:

require ["fileinto", "reject", "vacation", "imap4flags", "notify", "envelope", "body", "relational", "regex", "subaddress", "copy", "mailbox", "mboxmetadata", "servermetadata", "date", "index", "comparator-i;ascii-numeric", "variables", "editheader", "duplicate", "vacation-seconds"];

Additional Sieve help pages

Sieve is complicated, so we have some additional help pages available.

For a guide to the general basics of using Sieve, see our How to use Sieve help page.

For an FAQ on using Sieve, see our Sieve frequently asked questions help page.

For some specific examples of scripts that you can use Sieve in Fastmail, see our Sieve examples help page.

Was this article helpful?
37 out of 52 found this helpful