Server-Side Mail Rules

November 04, 2013 by Gabe | [mmd] |

Mail rules are the life raft of the modern working nerd. Without my mail rules I'd be overwhelmed and drowning in messages that I don't need to see right now, or maybe ever.

For years, I've enjoyed some very complex rules in With the combination of MailTags and Mail-Act-On (I've written about some of them here), an Mac server can become a competent virtual assistant. in Mavericks brings further improvements in rule handling.

The significant downside to this setup is that it requires an always-on Mac with Mail running. When I started moving to FastMail earlier this year I also forced myself to move my mail rules off of my Mac and onto the mail host.1


iCloud is no longer my mail server of choice but it's still common and it's also easy to overlook the option for server-side mail rules. That's right, while most people probably assume they must use to setup rules, if you visit, you can configure basic mail rules on the iCloud server. These rules are separate and supersede the rules on your Mac.

The iCloud rules can trigger on one of a few message properties. The most significant omission in this list is message body text matching.

I was able to move about half of my rules to the iCloud server. That's still an improvement. The rules execute fast and work even when my Mac is shutdown.

You can also change the order of execution by dragging them up and down the list. This way you can forward a message before filing it into a folder.

The lack of powerful triggers and sophisticated processing options leaves holes in my workflow. Luckily, I was well on my way toward moving completely to FastMail.2


Apple's mail rules are, well, Apple's secret. They provide a user interface for building the rules, but the underlying technology is their secret.

FastMail's mail rules are built on the Sieve technology for mail filtering which uses a fairly simple syntax and collection of verbs/functions to manipulate incoming mail messages. For example, this basic syntax triggers on messages with body text "the tracking number is" and then forwards it to Junecloud for tracking.

if body :text :contains  "the tracking number is" {
  redirect :copy "";

I'm sure this is making Apple's mail rules look much more appetizing right now. Who wants to write their mail rules by hand? FastMail takes care of that for you with a GUI to the rules. In fact, FastMail gives you three different ways to build mail rules, based on how much control you want and how comfortable you feel with the details.

At the most basic level you build rules by clicking and choosing from drop-down menus. For example, building forwarding rules is simple. Just click a and add the trigger using trivial expressions. Want to build a mail forwarding rule based on body text? Done:

But now you can stack your rules up. What about moving the message after it's been forwarded? Just add another rule a little further down with the same trigger but with an Organize function:

FastMail lets you also flag and pin messages after they have been filed.

The second way to build mail rules in FastMail is hidden under the Advanced menu. In this mode, you get access to two additional options, Reflection and Show All.

Mail reflection is similar to forwarding but it allows the receiving account to reply with the original recipients address. For example, reflect a message from your FastMail account to your iCloud account. Now when you reply to the reflected messages from within iCloud, it looks as if you used your FastMail account. It's a nice way to redirect messages and be able to reply without giving away your iCloud account address.

The Show All option simply shows all of the mail rules you've created so that you can see what order they will execute in. Organizing messages into folders is always executed last.

Regex Matching

So, we're feeling pretty good about or simple mail filters. They firing off and moving messages around like we know what we're doing. But so far the mail rules are looking for specific phrases or sender addresses.

FastMail allows regular expression matching to trigger rules too.3 Just choose the matches glob pattern after entering the expression between pipe characters, |, to start making some magic happen.

Like a Sieve

As mentioned earlier, under the hood, FastMail is using the Sieve syntax. If you want to see what's going on, head back to the Advanced settings and scroll down to the bottom of the mail rules page. What you will see is the code equivalent of your mail rules. FastMail rules are portable.

if body :text :contains  "the tracking number is" {
  redirect :copy "";
if body :text :matches  "*/\\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\\dT]\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d)\\b/i*" {
  redirect :copy "";
if body :text :matches  "*/\\b((96\\d\\d\\d\\d\\d ?\\d\\d\\d\\d|96\\d\\d) ?\\d\\d\\d\\d ?d\\d\\d\\d( ?\\d\\d\\d)?)\\b/i*" {
  redirect :copy "";
if body :text :matches  "*/\\b(91\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d|91\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d\\d)\\b/i*" {
  redirect :copy "";

Select and copy the Sieve code. You can then past this into any other Sieve-based email filtering system and get the exact same behavior. The above is just a snippet of my mail rules for redirecting messages that contain tracking-number-like strings.

Sieve has several revisions and many features. FastMail only supports a subset. Unfortunately, while FastMail supports the imapflag module, there's not much you can do with it. Imapflag does what you expect, it manipulates IMAP message flags. FastMail only supports a single flag type, so the best you can do is mark and unmark a message.


This is a good start for me. I'm building mail rules that I can take with me to many other mail servers. I've rebuilt all of my message forwarding, junk-filter and message filing rules right on the FastMail server. In addition, Sieve is a technology I feel good about and doesn't box me into a proprietary, non-portable set of rules. It just lives on my mail server and lets me get use my attention somewhere other than in my mail application.

  1. It wasn't a hard decision. I left my Mac off for a couple of days while at a meeting and was disgusted by the amount email in my inbox. When you rely on automation, it's easy to forget how awful the real world is. 

  2. Use this affiliate link to get an awesome mail server and support 

  3. You can access the "glob" mode by pasting in some basic regex between pipe characters and preceded by /*. It will look like this "|/*|". It only shows up in the dropdown if you know how to enter the regex rule. It's like a little intelligence test to see if you're ready for the power.