Sub topic 10
Automating sorting with filters and rules
This chapter assumes you already do the following:
- Use an email client such as Gmail, Outlook or Apple Mail every day.
- Know how folders or labels, archiving and search work in that client.
- Follow the per-message decisions from earlier chapters: delete, do, delegate, defer, file.
It also assumes you have unsubscribed from mail you no longer want. Filters handle what is left.
What filters and rules do
A filter (Gmail's term) or rule (Outlook's and Apple Mail's term) is an instruction your mail system runs automatically on each message as it arrives. It has two parts:
- A condition, such as "the sender is [email protected]" or "the message has a List-Unsubscribe header".
- An action, such as "move to the Receipts folder", "mark as read" or "apply the label Newsletters".
Filters apply your triage decisions without you making them each time. They fit the cases where the right decision is the same every time. They do not replace judgment about mail that needs a human decision.
The inbox is a queue of items that need a decision. A filter takes items out of that queue when the decision is predictable. Newsletters can go to a folder you read in a scheduled session. Receipts can be archived. Alerts you only check on request can skip the inbox.
Choosing what to automate
Automate a message type only when you can state the decision as a rule you would never override. A useful test is to ask: "If I saw this message, would I always do the same thing?" If the answer is yes, write a filter. If it is "usually", leave it in the inbox.
Good candidates:
- Newsletters and digests. Send them to one folder or label and read them in a session.
- Receipts and order confirmations. Label them and archive them. You find them later by search.
- Automated notifications, such as build results, calendar replies and social network alerts. Skip the inbox, or mark as read if you only need them as a record.
- Mail from a mailing list. Send each list to its own folder or label.
- Messages from a few important people. Star them or apply a label so they stand out. This is the one case where a filter adds attention instead of removing it.
Poor candidates:
- Anything where a wrong guess costs you. A filter that deletes mail on a loose keyword will eventually delete something you needed.
- Mail from people who write about many different topics.
- Mail that needs a reply by a deadline. If it skips the inbox, you may not see it in time.
Writing conditions that match the right mail
The quality of a filter depends on how precise its condition is. Compare these conditions, from least to most reliable:
| Condition | Reliability | Typical problem |
|---|---|---|
| Subject contains "sale" | Low | Also matches "Wholesale pricing for your contract" |
| Sender domain is `shop.example.com` | Medium | A real person at that company writes to you too |
| Sender address is `[email protected]` | High | The sender may change the address |
| Header `List-Unsubscribe` exists | High for bulk mail | Some transactional mail also carries it |
Some practical points:
- Prefer the sender address over the display name. Display names are easy to fake and often change.
- Combine conditions to narrow a filter. "From the shop domain AND subject contains 'receipt'" is safer than either part alone.
- Use headers when your client supports them. Bulk mail usually carries a
List-Unsubscribeheader, and mailing lists carryList-Id. These headers are more stable than subject wording. - Do not rely on filters to catch security-sensitive mail. Password resets and bank alerts should reach the inbox.
A rule written in Sieve
Sieve (defined in RFC 5228) is a standard language for server-side mail filtering. Many mail servers support it, and some clients let you edit rules in it. Even if you use a point-and-click interface, reading Sieve shows what a rule does: a test, then an action.
This rule sends bulk mail that has a List-Unsubscribe header to a Newsletters folder, so it stays out of the inbox queue.
require ["fileinto"];
# Bulk mail that carries a List-Unsubscribe header goes to a folder
# you read in a scheduled session, not into the inbox.
if exists "List-Unsubscribe" {
fileinto "Newsletters";
}Two details matter here:
require ["fileinto"];declares the extension that provides thefileintoaction. Without it, the script fails to load.fileintofiles the message in the named folder and cancels the default action of keeping it in the inbox. If the folder does not exist, behavior depends on the server. Some create it, and others fall back to the inbox. Create the folder first.
A narrower rule with two conditions might look like this:
require ["fileinto"];
if allof (
address :is "from" "[email protected]",
header :contains "subject" "receipt"
) {
fileinto "Receipts";
}allof means all tests must pass. anyof means at least one must pass. Use allof to narrow and anyof to group several senders under one action.
Filters are the same idea as your triage function
Earlier in this tutorial, triage was expressed as a function that returns one action for each message. A filter is that function with the answers supplied by the message itself, not by you. The sketch below shows the idea in Python. It is not an email client feature. It shows how a rule table maps conditions to actions and how order matters.
def triage(is_junk, needs_action, minutes_needed, someone_else_better, worth_keeping):
"""Return the action to take for one message."""
if is_junk:
return "delete"
if not needs_action:
return "archive" if worth_keeping else "delete"
if someone_else_better:
return "delegate"
if minutes_needed <= 2:
return "do now"
return "defer to task list"
print(triage(False, True, 1, False, True)) # do now
print(triage(False, True, 30, False, True)) # defer to task list
print(triage(False, False, 0, False, True)) # archiveA filter takes over the first branches of this function for known senders. It decides "not actionable, worth keeping, so archive" without you. Messages that no rule matches fall through to you, and you run the full function by hand.
Order, conflicts and stopping
When several rules could match one message, the result depends on how your client combines them.
- Gmail applies every matching filter. If two filters apply different labels, the message gets both. If they conflict, for example one archives and one does not, the outcome can be surprising.
- Outlook runs rules from top to bottom. A rule can include "Stop processing more rules". Without it, later rules still run.
- Sieve runs tests in script order. A
stop;command ends the script. Afileintofollowed by furtherfileintocommands files copies in each folder.
Guidelines that avoid conflicts:
- Keep each rule narrow and give it one purpose.
- Put specific rules before general ones, and stop processing after a specific match.
- Name rules and labels so you can tell what they do months later.
- Avoid a catch-all rule that moves everything not otherwise matched. It hides mail you have not classified.
Testing a new filter
Do not write a filter and trust it. Check it first.
- Search with the same condition before you create the rule. Gmail's filter dialog has a "Search" button for this, and Outlook and Apple Mail let you run a search with the same terms.
- Read the results. If you see mail you would not want filtered, tighten the condition.
- Create the rule with a safe action first, such as applying a label without archiving.
- After a few days, review the labeled mail. When it is all correct, add the action that skips the inbox.
- Apply the rule to existing mail only after step 4, if your client offers that option.
Maintaining your rules
Filters decay. Senders change addresses, you stop caring about a list, and a rule written for a project outlives the project.
- Review the filter list during your weekly review. Delete rules that no longer match anything.
- Check the folders that rules feed. A Newsletters folder with 400 unread items means the rule is working but your reading session is not.
- When you unsubscribe from a sender, delete its filter. Otherwise it stays as clutter.
- If you find yourself searching a filtered folder for something important, change the rule so that kind of mail reaches the inbox.
Common errors
- Filtering too early. Wait until you see a pattern at least three or four times before you write a rule.
- Using subject keywords as the only condition. They match more than you expect.
- Deleting automatically. Prefer labeling or archiving, so the message stays searchable. Delete only for senders you have already unsubscribed from and who keep sending.
- Forgetting that rules run only on new mail. A new filter does not clean the backlog unless you apply it to existing messages.
- Relying on a rule for mail you are waiting on. Replies from a person should reach the inbox so you notice them.
Exercise
You get a weekly digest from [email protected], and you read it on Friday afternoons. You also get occasional personal messages from the same domain. Write the condition and action for a filter, and say how you would check it before turning it on.
Show answer
Condition: the sender address is exactly [email protected]. Using the full address, not the domain, keeps personal messages from colleagues at team.example.com in the inbox. Action: apply the label or folder Newsletters, and skip the inbox once the rule is verified. To check it, search for from:[email protected] first and confirm that every result is a digest. Start with the label only, review it after a few days, then add the skip-inbox action. Add a Friday-afternoon slot to your processing sessions so the folder is read.
Exercise
In Sieve, what is the difference between allof and anyof, and which would you use to file mail from either of two addresses into one folder?
Show answer
allof requires every test to pass, and anyof requires at least one. To file mail from either of two addresses into one folder, use anyof with an address test for each sender, followed by a single fileinto action.