# Rules

> What rules are, where and when they run, the order they run in, a first rule step by step, and how Postern keeps rule actions safe.

Source: https://docs.posternmail.app/rules/

Rules act on new mail the moment it arrives: they tag, move, forward, redirect, answer, keep or
report it. This page explains how rules work as a whole and walks you through a first rule. The
pages under **Rules** in the sidebar cover each part in detail.

You find rules in the web app under **Settings → Rules**. The same group in the settings holds
**Rule activity**, **Captured mail** and **Sending queue**.

![The rule editor with a redirect rule in Test mode and the "Test against recent mail" panel](https://posternmail.app/img/rules.webp)

## Where and when rules run

Rules run on your [Hub](https://docs.posternmail.app/how-it-works/), not in an app. The Hub syncs your mailboxes all the
time, so a rule acts on new mail within moments of its arrival, also while your computer and
phone are switched off.

- A rule runs once for each **new** message, right after the Hub has stored it and before it
  announces the message to your devices. A rule that only tags or moves mail is therefore done
  before your phone shows a notification.
- Rules do not run again on mail you already have. To apply a rule's simple actions to existing
  mail, use **Save and apply to mail I already have** in the editor (see
  [below](https://docs.posternmail.app/rules/#apply-a-rule-to-mail-you-already-have)).
- A rule that needs the message text, an attachment name or an unusual header (see
  [Conditions](https://docs.posternmail.app/rules/conditions/#conditions-that-need-the-message-text)) is decided a little
  later, after the Hub has fetched the message body. The editor says so under such a condition:
  "This needs the message text, so the rule may run a little later."

> [!NOTE]
> The desktop and mobile apps connect to your mail providers directly. If your Hub has been
> unreachable for more than two minutes, an app runs your rules itself for mail that arrives in
> the meantime, but only the steps that change a message (tag, move, mark, star, Trash).
> Everything that sends mail, keeps a copy, deletes for good or calls out (redirect, forward,
> automatic reply, capture, copy, delete for good, web address, notification) runs only on the
> Hub. Running a rule twice does no harm: actions are done once per message.

## What a rule is made of

| Part | In the editor | What it means |
|---|---|---|
| Name | **Name** | Shown in the rule list, the activity log and the sending queue. Up to 200 characters. |
| Mode | **Mode**: **Live** or **Test** | Live rules act. Test rules only write what they would do into the activity log. See [Test mode](https://docs.posternmail.app/rules/test-mode/). |
| Mailboxes | **In mailboxes**: **All** or one or more mailboxes | Which mailboxes the rule watches. **All** includes mailboxes you add later. |
| Conditions | **When**, with **all of these match** or **any of these matches** | What a message must look like. Up to 20 conditions. See [Conditions](https://docs.posternmail.app/rules/conditions/). |
| Exceptions | **Except when** | If **any** exception matches, the rule does not apply. Up to 20 exceptions. |
| Search | **Advanced: match with a search** | Optional. A search in the same language as the search box. When set, mail must match the search **and** the conditions. See [Search](https://docs.posternmail.app/using/search/). |
| Steps | **Then** | What the rule does, in order. See [Steps](https://docs.posternmail.app/rules/steps/). |
| Stop | **Skip the rules below this one** | When this rule matched, later rules do not run for the message. |

A rule needs a name, at least one condition or a search, and at least one step. The switch next
to a rule in the list turns it on or off without deleting it.

## Order of evaluation

Rules run from top to bottom in the order of the list. Use the arrows next to a rule to move it.
For each new message the Hub does this:

1. It skips rules that are off and rules that do not watch the message's mailbox.
2. It checks each remaining rule: conditions, then exceptions, then the search (if any).
3. **Every** rule that matches acts, not only the first one. If a matching rule has **Skip the
   rules below this one** and is Live, the rules below it are not checked for this message.
4. Within a rule, steps run in the order shown. The one step that takes the message out of its
   folder (redirect, capture, delete, archive, move, Spam) always runs last, whatever its
   position. A rule can have only one such step.
5. When a step takes the message away for good (redirect, capture, delete), rules further down
   do not run for it. Their entries in the activity log say "The message was already removed by
   another rule".

When several matching rules move the message, only one move happens. Trash wins over Spam,
Spam over a move to a folder, a move over Archive. Between two moves to folders, the lower rule
wins. The other rules' moves show "Another rule moved the message" in the activity log. Tags,
read and star changes of all matching rules are all applied.

> [!TIP]
> A rule in Test mode never stops later rules, even with **Skip the rules below this one**
> ticked. The editor says "While the rule is in Test, the rules below still run." This lets you
> test a new rule above your existing ones without changing what they do.

## Your first rule, step by step

This example tags receipts from one shop and archives them.

1. Open **Settings → Rules** and choose **New rule**.
2. In **Name**, type `Receipts from Acme`.
3. Leave **Mode** on **Live**. Under **In mailboxes**, keep **All** or pick one mailbox.
4. Under **When**, the first condition is already there. Set it to **Address**, **From**,
   **is at domain**, and type `acme.com`. This matches `billing@acme.com` and
   `noreply@shop.acme.com`.
5. Choose **Add a condition**, set it to **Text**, **Subject**, **contains**, `receipt`. Leave
   the match setting on **all of these match**.
6. Under **Then**, choose **Add a step** → **Tag** and pick a tag. Then **Add a step** →
   **Archive**.
7. Look at **Test against recent mail** on the right. It shows how many of your last 50
   messages match and what the rule would do to each. Nothing is changed.
8. Choose **Save**. The rule list now shows the rule with the line "From is at domain “acme.com”
   and subject contains “receipt”".

> **Check:** the next receipt from Acme arrives tagged and archived. **Settings → Rule activity**
> lists the rule's actions with the outcome **Done**, and the rule list shows "matched 1 time".

> **If it did not work:** open **Rule activity** and filter by the rule. No entry means the rule
> did not match: compare the message with the conditions in **Test against recent mail**. An
> entry with **Failed** says why in its detail line, for example when the mailbox needs you to
> sign in again.

### Apply a rule to mail you already have

**Save and apply to mail I already have** saves the rule and runs it on the newest 10,000
matching conversations you already have. Only the simple actions run: tag, mark as read, star,
archive, move, Spam, Trash. Steps such as redirect, forward, reply, copy, capture, delete or
calling a web address never run on old mail. A rule in Test mode only counts: "Rule saved. It's
in Test, so nothing was changed: 12 conversations you already have match."

## How Postern keeps rule actions safe

Rules can send mail and remove messages from your mailbox. A mistake there costs more than a
wrong tag, so these actions have extra protection.

### A copy first

Before a rule removes a message for good (redirect, capture, delete for good), the Hub stores an
encrypted copy, reads it back and checks it. Only then is the message deleted from the mailbox.
You can put it back from **Captured mail**, the **Sending queue** or with **Undo** in the
activity log. See [Captured mail](https://docs.posternmail.app/rules/captured-mail/).

### Nothing is lost in a restart

Every rule action that sends, keeps or calls out is written to the Hub's database before it
starts, and every stage of its progress is recorded. If the Hub stops (an update, a power cut),
it carries on where it stopped when it starts again. See [Sending queue](https://docs.posternmail.app/rules/sending-queue/).

### It retries, then asks you

A send or a web call that fails is tried again after 1 minute, 5 minutes, 30 minutes, 2 hours and
6 hours. After that it waits for you in the sending queue with **Retry**, **Change recipient**,
**Restore to mailbox** and **Download .eml**. If the Hub cannot tell whether an interrupted send
went out, it checks your Sent folder, and if that does not settle it, it asks you rather than
risk sending the message twice.

### Limits and loop guards

Rules that send mail stop at daily limits (100 a day per rule, 200 per mailbox) and pause for 15
minutes when more than 20 go out in a minute. They never send on mail that a rule sent, automatic
replies or bounces, and never to the mailbox's own address. See [Limits and guards](https://docs.posternmail.app/rules/limits/).

### Test mode

New rules that send mail start in Test mode. They write what they would do into the activity log
and change nothing. See [Test mode](https://docs.posternmail.app/rules/test-mode/).

### Alerts

When rule mail needs you (sending paused, a message that could not be sent or bounced, a mailbox
that needs you to sign in, a web address that never answered), the Hub raises an alert. Open
alerts appear at the top of **Settings → Rules** under **Needs your attention**, your devices get
one notification, and you can have alerts sent to ntfy, Discord or Gotify. See
[Alerts](https://docs.posternmail.app/rules/alerts/).

## The pages in this section

| Page | What it covers |
|---|---|
| [Conditions](https://docs.posternmail.app/rules/conditions/) | Every condition and exception, operators, patterns, time of day and time zone |
| [Steps](https://docs.posternmail.app/rules/steps/) | Every step, what it does on Gmail, Microsoft, IMAP and JMAP, and its risks |
| [Test mode](https://docs.posternmail.app/rules/test-mode/) | Test mode, "Test against recent mail", switching a rule to Live |
| [Activity and undo](https://docs.posternmail.app/rules/activity/) | The activity log, how long it is kept, what Undo can and cannot reverse |
| [Sending queue](https://docs.posternmail.app/rules/sending-queue/) | Stages, retries, unconfirmed sends, Retry, Change recipient, Restore, Download |
| [Captured mail](https://docs.posternmail.app/rules/captured-mail/) | Mail the Hub keeps for rules: encryption, retention, restore |
| [Limits and guards](https://docs.posternmail.app/rules/limits/) | Daily limits, the burst pause, loop and bounce guards |
| [Alerts](https://docs.posternmail.app/rules/alerts/) | ntfy, Discord and Gotify, what raises an alert |
| [Webhooks](https://docs.posternmail.app/rules/webhooks/) | The **Call a web address** step and its JSON payload |
| [Import from MailManager](https://docs.posternmail.app/rules/mailmanager/) | `postern-hub import-mailmanager` |
