SieveFAQ

From FastMailWiki

Jump to: navigation, search

This Sieve FAQ tries to answer some of the questions that frequently arise regarding the use of the Sieve mail filtering language, specifically in the context of the Fastmail service. Questions are categorized in 3 categories: "General Sieve", "Advanced Sieve" and "Fastmail Specific".

Syntax examples are inline in monospaced font.
X-Header-Name is a placeholder for header names in tests.
Links in this FAQ to specific EmailDiscussions post open the post in a "View Single Post" window. If you want to view the thread the post is in, click on the timestamp link at the lower-left corner of the post (to the left of the "profile" link).

Contents

General Sieve

What the heck is Sieve, anyway?

Sieve is a computer language designed to filter email messages. A Sieve script generally consists of a series of tests and accompanying instructions, which are executed whenever the mailserver receives an email. If a test succeeds (ie: Sieve determines that the outcome of a comparison is True) the instruction(s) are executed. There are many instructions, but the most commonly used one is filing the mail in a specific folder. Tests can be nested, allowing you to create complex control structures to guide your mail to a specific destination.

Why the name 'Sieve'?

A sieve in its original context is a device used for filtering coarse material from a mixture. This is a goal that is accomplished, albeit more abstractly, by the Sieve filtering language. By looking at certain characteristics of emails, some are filtered out and others are let through.

Where can I learn how to use Sieve?

This FAQ is not going to teach you the ins and outs of writing Sieve scripts. However, we can point you at some sites and methods to learn that may help you gain an understanding of the Sieve language. First of all, when you use Fastmail you can opt to write your scripts using an interface instead of using Sieve directly. You could try writing some simple rules using this interface, and then look at the corresponding Sieve code to see how it works.
If you're into the technical-reading thing, you could give RFC:3028 a shot, as it is the authorative definition of the Sieve language.
There is the unfinished BasicSieve page on this wiki. SieveExamples is the most popular page on this wiki, and the first one about SIEVE, and is linked to heavily.

Why use Sieve?

Sieve is a language designed specifically to become the standard language for managing email flows. If you have access to a GUI for creating your Sieve script and it provides all the power you need, by all means, use it. Only write your own Sieve code if a GUI interface to Sieve you have access to doesn't provide the power you need, but Sieve itself does.
Sieve scripts can be used to automatically delete or forward messages, to send autoreplies, to sort them in folders, (on IMAP servers) to mark messages as read or flagged, to test messages for spam or viruses, to reject messages at or after delivery, and even to replace MIME parts of a message. Most of these uses require that one or more extensions be supported.

What are Sieve extensions? Which SIEVE implementations support which extensions?

RFC:3028 defines a set of comparators, tests, and actions that must be present in every Sieve implementation. Extensions make Sieve more powerful by extending this set.

When such an additional capability is used in a Sieve script, this must be declared by included the Capability String for this extension in the require statement (which must be the first statement in a script).

The SieveExtensionsSupportMatrix shows which SIEVE implementations support which extensions, and contains links to the latest versions of known Sieve extensions.

Can I detect similar messages with Sieve?

No. Sieve scripts run independently for each message received; there is no "memory" of past messages.

Can I use Sieve to filter messages I send?

No. Sieve only scans incoming messages (see delivery process).

I'm looking for examples of other people's scripts.

See SieveExamples, and/or search this wiki and the FastMailForum for "Sieve", and/or the references section of this page.

I use a client, can I still use Sieve?

Yes. Sieve scripts, like spam scanning, are executed on the server, before the message is delivered to your account. Whether you use the Web interface, or POP/IMAP client(s), you can still use Sieve. This also means you can use Sieve even if you don't check your mail often.

Where can I get more help regarding Sieve?

Please see our references section. Besides that, we can also recommend searching the Fastmail Forum. Many people have come there asking questions and finding answers. It's very possible that your question has already been answered there. If it's not, feel free to ask for help there. However, please read the beginning of the "Please read this BEFORE posting a Sieve-related question" thread first!

Advanced Sieve

Messages are filed into one folder and I want them filed in multiple folders, or vice-versa.

Use stop, else (and elsif) to stop execution or to only execute one of several alternatives.

Messages are filed to the Inbox where they should be filed to a folder.

Make sure you typed the folder name correctly (INBOX. followed by a copy-paste of the name in the "Rules" screen, or just INBOX for the Inbox). Folder names are case-sensitive (EmdPost:281598). In addition, if you've renamed a folder, make sure to upadte you relevant sieve scripts, otherwise messages intended for this folder will be filed into the Inbox.

I want to match messages to the domain with catchall, unless they were sent to a specific localpart.

Make sure the tests for specific localparts are placed before the catchall test. For example, see this thread (includes both GUI instructions and code).

How do I match an exact string?

Use the :is comparator. If you prefer regexes, the characters matching start- and end-of-string are ^ and $, respectively.
Four different ways for matching a string then a string of which it is a substring are in this forum thread.

Matching empty and non-existing fields

The exists test should be used to test for the existence of a field in the header; it is equivalent to the test not header :contains "X-Field-Name" "". One way to describe an empty field is header :is "X-Field-Name" "" (alternative forms use regex or :matches with exists). WARNING: Actually, matches to "" sometimes don't work where they should.

I already have a script. I want to forward all my mail to a backup account. How do I do this?

Make sure that either an explicit keep or a fileinto, but not both, are executed for every message (this is in addition to the redirect).

How do I apply an action to all messages?

Use the true test (example: if true { /* Do action */ }), or don't use a test at all (remove the "if", the test, and the brackets from the previous example). In the GUI, the "Always" test is available.

Can I mark messages from a Sieve script?

Yes. The "imapflags" extension is supported (though, as all extensions, it must be explicitly activated by adding "imapflags" to the require statement at the top of the script.)
To mark a message as read, IMAP deleted ("crossed out"), or flagged, use addflag "\\Seen", addflag "\\Deleted", and/or addflag "\\Flagged", respectively.

What are regular expressions (regex)?

Regular expressions are basically a much more powerful version of wildcards.
Reference and syntax: the "regex" extension.
If you are new to Sieve but familiar with regular expressions, you may find it useful to enter regular expressions directly from the Define Rules screen.

Can I use a catchall and detect invalid addresses (random@mydomain) without manually listing all valid address?

Yes, by using checksums.

Fastmail Specific

Can I forward messages to my cell phone?

Yes. Reference: "notify" extension syntax

Can I forward just a summary (ie, "from" + "subject") but not the entire email?

Yes, using the "notify" extension . See EmdPost:284042 and EmdPost:284043 in this thread for an example.

Why do I get copies in the Inbox when using forwarding rules?

This is due to the interaction of redirect, fileinto, and keep; it is a common issue, and will be resolved when Fastmail finishes implementing the "copy" extension. Meanwhile workarounds avoiding the duplicates problem are known: for those who have customized their script manually, a workaround is documented here, and for users of the Define Rules GUI screen this post describes a setup that avoids the problem. The latter breaks automatic SubDomain-addressing/PlusAddressing filing. Filing rules created in the "Filing into folders" section of the GUI are not affected.

Do I have to create two rules if I want to file a message into two folders?

If you use the GUI: Yes, unless you also use tricks.
If you use a manually-edited script: No, just add a second fileinto (or any other action) immediately after the first one.

Can I modify the header or body of a message with Sieve?

No. Fastmail's CyrusImapServer does not yet support the draft "editheader" and "remove-attach" extensions.

Does Fastmail support "managesieve"? Do I have to log in to the Web interface to edit my script?

Fastmail does not yet support the "managesieve" extension; currently, the only way to modify one's script is by logging in to the Web interface. See also forum posts by Jeremy and Rob.

Can I reject messages before delivery (before SMTP DATA)?

No. Fastmail runs Sieve after the SMTP connection is closed (delivery process). The relevant extension is "refuse", and is still in draft status.

Can I use variables in my script?

No. Fastmail does not yet support the draft "variables" extension. (There are a few "variables" used with the "notify" extension , but probably not what this question is asking for.)

Can I filter on message body?

No. Fastmail's CyrusImapServer does not yet support the "body" extension (which is still in draft status); currently, Sieve can only see the message header. A way around this, for Full and Enhanced members, is to look for SpamAssassin hits related to the message body. This extension is also very resource intensive.

Are there Fastmail-specific header fields I can use in my script?

Yes. See this question on the Fastmail FAQ.

I want to test my script without sending many test messages.

Use SanjaySheth's Sieve Tester.

The Sieve tester's predictions are wrong.

The Fastmail Sieve Tester does not support all extensions - specifically, it does not fully support "regex" and "envelope" (the workaround for the latter issue is to test the FM-Specific fields X-Delivered-To, X-Resolved-To and X-Mail-From).

It DOES support regex, at least partially.

References

General documents

  1. Sieve specification
  2. The IETF SIEVE Working Group charter
  3. Cyrusoft's Sieve homepage
  4. User:MrElvey's Sieve Webpage (see discussion)
  5. SanjaySheth's Fastmail Sieve Tester
  6. SpamAssassin and Sieve filterscripts
  7. Sieve support for emacs
  8. For Geeks: Sourcecode for Cyrus and Sieve
  9. SieveExtensionsSupportMatrix
  10. Search for Regex-based sieve examples on this wiki

Extension specifications

See SieveExtensionsSupportMatrix for links to most recent versions of all extensions.

  1. imapflags rev. 3 ("imapflags")
  2. imapflags rev. 6 ("imap4flags")
  3. notify rev. 1
  4. Fastmail-specific "notify" documentation