Rule examples

Rules that work together.

One rule moves one kind of mail. These recipes combine them: shared lists, senders your provider vouches for, tags that later rules route on, the time mail arrived, what is attached, and actions that wait. Every key is in the rules reference.

New to Postvak? It sorts any IMAP inbox with rules like these, on macOS and Linux. Install it and write your first rule.

Order matters. Rules run in file order, top to bottom, on every sync. When several rules match a message, the first move or archive wins, and a stop keeps every rule below it away. So when you combine recipes, put the rules that tag or hold above the ones that move, specific rules above broad ones, and catch-alls last; rules coverage shows mail two rules compete for. Replace the .example domains and addresses, the account name and the folder names with your own.

Notifications first

A hold at the top of the file decides what your phone sees before any other rule runs.

Hold new mail for your phone

Mail apps on a phone mostly notify only for INBOX. A rule with stop near the top keeps every rule below it away from a message for a while, so the phone sees it before it is sorted. There are two ways to write it.

Hold everything for 15 minutes. Simple, but it also delays rules that only tag or mark read, and every route.

rules.toml
[[rules]]
name = "hold new mail for the phone"
match.younger_than = "15min"
actions.stop = true

Hold only what matters: banks, school, sign-in codes and security alerts. Everything else is sorted at once, and only the mail you would pick up your phone for waits. The subjects are two lists, since later recipes need the same patterns. Add the wording your own services use.

rules.toml
[lists]
sign_in_codes = { regex = "sign-?in code|verification code|magic link|inlogcode" }
security_alerts = { regex = "sign-?in alert|new device|password (was )?changed" }

[[rules]]
name = "hold important mail for the phone"
match.any = [
  { from_domain = ["bank.example", "school.example"] },
  { subject = { list = "sign_in_codes" } },
  { subject = { list = "security_alerts" } },
]
match.younger_than = "15min"
actions.stop = true

Pick one. To delay only a move and let tagging run, give that one rule an after, as in Delete CI mail once seen.

Trust and security

Mail that claims to be your bank, codes that pile up, and spam your server lets through.

Move only verified finance mail

Anyone can write paypal.com in a From line. authenticated holds only when your provider checked the signatures that prove a message came from that domain (DKIM and DMARC). Real bank mail is filed, and a lookalike stays in the inbox, flagged. One [lists] entry feeds both rules.

Keep the second rule. Without it, mail that fails the check stays in the inbox unflagged, looking just like the real thing next to it.

Needs the account's trusted_authserv_ids. Without it no mail counts as authenticated: the first rule never fires, and the second flags all of it. The tag needs a server that accepts custom keywords.

rules.toml
[lists]
finance = ["bank.example", "paypal.com"]

[[rules]]
name = "finance, verified"
match.from_domain = { list = "finance" }
match.authenticated = true
actions.move = "Finance"
actions.stop = true

[[rules]]
name = "finance lookalikes"
match.from_domain = { list = "finance" }
match.authenticated = false
actions.tag = "$Suspicious"
actions.flagged = true
actions.stop = true

Check that it works before you rely on it. rules check warns about an account without the setting. headers on a mail you received shows the server name after Authentication-Results:, which account trust adds. Then rules test should list a real bank mail under the first rule, not the second.

$ postvak rules check
$ postvak headers UID
$ postvak account trust ACCOUNT mx.example.com
$ postvak rules test "finance, verified"

Flag a name that claims to be your bank

The lookalike rule above catches mail that claims a finance domain in its address. Phishing more often comes from a domain of its own, with "PayPal" only in the display name, which is all most phones show. from_name reads that name alone. This rule flags mail whose name contains one of your finance names, unless it comes from one of the finance domains and your provider vouches for it.

Put it below the two finance rules. If a real service of yours mails under that name from another domain, such as a newsletter service, add that domain to finance.

Needs the account's trusted_authserv_ids, as in the rules above. Without it no mail counts as authenticated, so this flags every message with one of those names. The tag needs a server that accepts custom keywords.

rules.toml
[lists]
finance_names = { contains = ["Example Bank", "PayPal"] }

[[rules]]
name = "names that claim to be finance"
match.from_name = { list = "finance_names" }
match.none = [{ from_domain = { list = "finance" }, authenticated = true }]
actions.tag = "$Suspicious"
actions.flagged = true
actions.stop = true

Keep security mail out of the automated pile

Tag in one rule, route on the tag in the next: a rule sees what the rules above it changed in the same sync. The first rule tags security alerts and sign-in codes. The second clears mail from no-reply addresses out of the inbox, except what the first one tagged, so a sign-in code from a no-reply address stays where you will see it. The second rule is broad: in one file, put it below rules that file specific no-reply mail, such as the parcel chain.

It reuses both subject lists from the hold. If you paste a regex into a second rule, make it a list instead: copies drift apart without anyone noticing, and a list changes every rule that uses it at once.

Tags are IMAP keywords, which Thunderbird and other clients show as tags. They start with $ here, a common convention for keywords; any name without spaces works.

Needs a server that accepts custom keywords.

rules.toml
[lists]
bots = { regex = "no-?reply|notifications?@" }

[[rules]]
name = "tag security mail"
match.any = [
  { subject = { list = "sign_in_codes" } },
  { subject = { list = "security_alerts" } },
]
actions.tag = "$Security"

[[rules]]
name = "automated mail out of the inbox"
match.from = { list = "bots" }
match.none = [{ tag = "$Security" }]
actions.move = "Automated"
actions.notify = false

Delete sign-in codes once used

A sign-in code or magic link is useless a minute after you use it, and they pile up. This deletes one an hour after you read it. It only works because rules run again on every sync: the message qualifies an hour after you read it, long after it arrived. A code you never read stays.

The rule uses sign_in_codes alone, not the security alerts, so a "new device" warning is never deleted. The bots sender check keeps a person who writes "verification code" in a subject out of it. A delete keeps an .eml copy, so postvak trash restore brings a code back.

rules.toml
[[rules]]
name = "delete used sign-in codes"
match.from = { list = "bots" }
match.subject = { list = "sign_in_codes" }
match.read = true
match.older_than = "1h"
actions.delete = true

Junk spam by its score, in two steps

Many servers score every message but only move it to Junk above a high threshold. SpamAssassin writes the score as stars in X-Spam-Level; some providers use their own header. Find yours with postvak headers UID on a spam and a legitimate message.

Then go in two steps. Tag at a low threshold first, and look at what rules test lists. Once a threshold catches only spam, junk above it. junk moves the message to the Junk folder whatever the server calls it, and tags it $Junk so the server's filter learns from it.

rules.toml
[[rules]]
name = "spam suspects"
match.header.X-Spam-Level = { regex = '^\*{2,}' }
actions.tag = "$SpamSuspect"

[[rules]]
name = "spam"
match.header.X-Spam-Level = { regex = '^\*{5,}' }
actions.junk = true

Flag archives from senders nobody vouches for

A zip, an ISO or a program is how malware usually arrives. attachment matches an attachment's file name, and any attachment may match. This flags such mail unless your provider vouches for the sender, so a colleague's zip stays quiet and a stranger's stands out. has_part catches the same mail by its MIME type, for a file whose name hides what it is.

Needs the account's trusted_authserv_ids, as in the finance rules. Without it no mail counts as authenticated, so this flags every message with such an attachment.

rules.toml
[[rules]]
name = "unvouched archives"
match.any = [
  { attachment = { regex = '\.(zip|rar|7z|iso|exe|scr)$' } },
  { has_part = "application/x-msdownload" },
]
match.authenticated = false
actions.flagged = true

Shopping and newsletters

Parcels, lists, promotions and invoices: most of an inbox, and most of the exclusions people get wrong.

Tag, hold, then route parcel mail

A rule in the middle of a chain can pause it. The first rule tags parcel mail by carrier or subject. The second stops at mail that is tagged, unread and less than a day old, so a new delivery notice stays in INBOX. The third moves everything tagged once the hold lets go: when you read it, or after a day.

Keyword lists need your mail's languages. In a Dutch mailbox an English-only list (delivered, on its way) misses most of it, so this one has bezorgd and onderweg too.

rules.toml
[lists]
carriers = ["postnl.nl", "dhlparcel.nl", "dpd.nl", "ups.com"]
parcel_subjects = { regex = "delivered|on its way|bezorgd|onderweg|pakket" }

[[rules]]
name = "tag parcels"
match.any = [
  { from_domain = { list = "carriers" } },
  { subject = { list = "parcel_subjects" } },
]
actions.tag = "$Delivery"

[[rules]]
name = "hold unread parcels"
match.tag = "$Delivery"
match.read = false
match.younger_than = "1d"
actions.stop = true

[[rules]]
name = "route parcels"
match.tag = "$Delivery"
actions.move = "Parcels"

With a single route, actions.after = ["read", "1d"] on the third rule says the same thing more plainly than the hold, or than a hand-written none = [{ read = false, younger_than = "1d" }]. Either way, mail you never read moves once a day has passed. The hold rule pays off when several rules below it route the same tag, or should all wait.

Move list mail out of the inbox

Mailing-list and bcc mail has an unsubscribe header and none of your addresses among the recipients. to_me = false checks To, Cc and Delivered-To against your address and aliases, so mail sent to you personally stays, even from a list. Placed above the promotions sweep, it takes the lists; the sweep takes the shop mail addressed to you.

rules.toml
[[rules]]
name = "list mail"
match.to_me = false
match.header.List-Unsubscribe = { regex = "." }
actions.move = "Lists"
actions.notify = false

Sweep promotions, keep the receipts

A promotions rule is usually the first one people write, and the exclusions are where it goes wrong: an order confirmation, a reply in a thread, a bounce or a sign-in code that happens to carry an unsubscribe link. Two lists hold the exclusions, written as literal strings in single quotes so the regex needs no extra backslashes. The tag from the security rule keeps sign-in codes out.

Move promotions to a folder, not to Junk: junk trains your server's spam filter, and newsletters you signed up for are not spam.

rules.toml
[lists]
transactional = { regex = "order|bevestiging|confirm|receipt|invoice|factuur|e-ticket" }
replies_and_bounces = { regex = '^\s*(re|fwd?|antw|doorst)\s*:|delivery status|undeliver' }

[[rules]]
name = "promotions"
match.header.List-Unsubscribe = { regex = "." }
match.none = [
  { subject = { list = "transactional" } },
  { subject = { list = "replies_and_bounces" } },
  { tag = "$Security" },
]
actions.move = "Promotions"
actions.notify = false

Add replies_and_bounces to the none of any rule that matches on subject keywords: a Re: about an invoice is a conversation, not an invoice.

File a shop that mails through a newsletter service

Many shops send promotions through a service such as Mailchimp or Sailthru, so the address is the service's domain and the shop's name appears only in the display name. from_domain reads only the address, from_name only the display name. This rule uses both on purpose, and keeps the shop's orders out with the transactional list.

A display name is whatever the sender writes, and anyone using that service can write "Brand". Fine for filing; never use it for mail you would act on, such as payments. To catch a name that claims too much, see Flag a name that claims to be your bank.

rules.toml
[[rules]]
name = "brand promotions"
match.any = [
  { from_domain = "brand.example" },
  { from_domain = "esp.example", from_name = { contains = "Brand" } },
]
match.none = [{ subject = { list = "transactional" } }]
actions.move = "Shopping/Brand"

Keep overdue invoices in view

Archive invoices, except reminders, whether the reminder is in the subject or only in the text. HTML mail is converted to text before the body regex reads it.

Needs the body of new mail in the rule's folder, which Postvak downloads for this rule. A message whose body could not be downloaded never matches, even through none, so it stays in the inbox.

rules.toml
[[rules]]
name = "overdue invoices stay put"
match.subject = { contains = ["invoice", "factuur"] }
match.none = [
  { subject = { contains = ["overdue", "reminder", "herinnering"] } },
  { subject = { list = "replies_and_bounces" } },
  { body = { regex = "payment (is )?overdue|betalingsachterstand" } },
]
actions.archive = true

Give each +tag its own folder

Sign up at each shop with its own tag, such as me+shop@example.com, and one rule files every tag into a folder of its own. {subaddress} in move is the tag, lower-cased, and a folder that does not exist yet is created. When a tag starts getting spam, you know which shop passed your address on.

Anyone can write to any tag, so only plain tags of letters, digits, - and _ count, and the folder must sit below one you name. Mail without a usable tag is not moved.

rules.toml
[[rules]]
name = "plus addresses to folders"
match.alias = "me+*@example.com"
actions.move = "Tags/{subaddress}"

Work

Colleagues, outsiders and the build bots in between.

File work mail, minus its bots

Nest none inside any: each entry of any is a set of conditions that must all hold, and can exclude mail of its own. This files mail from colleagues, but not their build bots, together with anything sent to your work alias. It reuses the bots list.

rules.toml
[[rules]]
name = "work, minus the robots"
match.any = [
  { from_domain = "work.example", none = [{ from = { list = "bots" } }] },
  { alias = "me+work@example.com" },
]
actions.move = "Work"

Mark mail from outside the team

to_domain reads the To header, from_domain the sender's address and never the display name. Together they tag mail sent to your company from anywhere else.

Needs a server that accepts custom keywords.

rules.toml
[[rules]]
name = "tag external mail to the team"
match.to_domain = "work.example"
match.none = [{ from_domain = "work.example" }]
actions.tag = "$External"

Quiet work mail after hours

Postvak knows when a message arrived, so a rule can treat the evening differently from the working day. Work mail that comes in at the weekend or outside office hours makes no notification and gets a tag, so on Monday you can find what came in. arrived_hours leaves out its end time and wraps past midnight when the end comes first; the none turns office hours into everything else.

Needs the right time zone where the daemon runs: arrival times are in that machine's local time. The Docker image runs on UTC unless you mount the host's with -v /etc/localtime:/etc/localtime:ro. The tag needs a server that accepts custom keywords.

rules.toml
[[rules]]
name = "work mail after hours"
match.from_domain = "work.example"
match.any = [
  { arrived_days = "sat-sun" },
  { none = [{ arrived_hours = "08:00-18:00" }] },
]
actions.tag = "$AfterHours"
actions.notify = false

Delete CI mail once seen

GitHub marks its build mail with an X-GitHub-Reason header. after takes a list and waits for whichever comes first: you read the message, or two days pass. A delete keeps an .eml copy in Postvak's trash, so postvak trash restore brings it back.

rules.toml
[[rules]]
name = "CI mail, once seen"
match.header.X-GitHub-Reason = { equals = "ci_activity" }
actions.delete = true
actions.after = ["read", "2d"]

Forward the invoice PDF to your bookkeeper

Invoice mail often carries a second PDF, such as the terms and conditions. The rule fires only when an attachment's name says it is an invoice, and the forward's attachments filter takes only PDFs with such a name, so the terms stay behind; the forward names what it left out. Postvak cannot pick "the first PDF": every attachment the filter matches goes along, so a mail with two invoices forwards both. authenticated keeps a stranger's fake invoice from reaching your bookkeeper.

Needs sending set up for the account, and your approval in Postvak: a forward rule sends nothing until you approve it with postvak rules approve or in the Rules view, and only mail that arrives after that is forwarded. Also the account's trusted_authserv_ids, as in the finance rules.

rules.toml
[[rules]]
name = "invoices to the bookkeeper"
match.attachment = { regex = '(invoice|factuur).*\.pdf$' }
match.authenticated = true
actions.forward.to = "books@example.org"
actions.forward.note = "Invoice for the records"
actions.forward.attachments = { type = "application/pdf", name = { regex = '(invoice|factuur)' } }
actions.move = "Finance"

Clean-up

Rules that tidy what is already sorted, and the one that goes last.

Tidy a folder that is not INBOX

A rule watches INBOX unless it names a folder, and runs for every account unless it names one. This one archives list mail you have read, two weeks on, for your personal account only, and leaves flagged threads alone.

rules.toml
[[rules]]
name = "tidy Lists"
account = "personal"
folder = "Lists"
match.read = true
match.flagged = false
match.older_than = "2w"
actions.archive = true

Archive the rest, last

A catch-all for INBOX: archive read, unflagged mail after 90 days. It goes at the bottom of the file. When several rules match a message, the first move or archive in the file wins, so a catch-all near the top would take mail that a more specific rule below it meant to file.

A rule only acts on mail that arrives after you enable it, so on its own this one does nothing for 90 days. To clear the backlog, run it once over older mail, with a dry run first.

rules.toml
[[rules]]
name = "archive the rest"
match.read = true
match.flagged = false
match.older_than = "90d"
actions.archive = true
$ postvak rules apply-existing "archive the rest" --dry-run
$ postvak rules apply-existing "archive the rest"

Try them before they act

A new rule acts only on mail that arrives after you enable it. Before that, check the file and preview each rule on the mail Postvak has cached. --as-of moves the clock, to see what older_than and after will do later. --as-arrival treats every cached message as if it just arrived, to see what a hold or an after wait does to new mail; with --as-of 20min beside it, what happens 20 minutes on. rules coverage shows mail that several rules compete for. The rules reference has more examples of single rules.

$ postvak rules check
$ postvak rules test "spam suspects"
$ postvak rules test --as-of 2d
$ postvak rules test --as-arrival --as-of 20min
$ postvak rules coverage --folder INBOX