# Conditions and exceptions

> Every rule condition and exception, with its fields, operators and value formats, pattern syntax, how addresses and headers are matched, automated mail, and time of day.

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

Conditions decide which messages a rule acts on. This page lists every condition, its operators
and what exactly each one compares. Exceptions use the same conditions.

## How conditions combine

- Under **When**, a rule applies when **all of these match** (the default) or **any of these
  matches**. The choice appears once the rule has two or more conditions.
- Under **Except when**, the rule does not apply if **any** exception matches.
- Under **Advanced: match with a search**, an optional search in the same language as the search
  box (`from:`, `to:`, `subject:`, `has:attachment`, `list:`, `-word`, `OR`, see
  [Search](https://docs.posternmail.app/using/search/)). When it is set, a message must match the search **and** the
  conditions. A rule can also use only a search and no conditions.

So a rule matches when: the search matches (if there is one) **and** the conditions match **and**
no exception matches.

| Limit | Value |
|---|---|
| Conditions per rule | 20 |
| Exceptions per rule | 20 |
| Length of a text value | 1 to 500 characters |
| Length of a header name | up to 100 characters |
| Length of the search | up to 2,000 characters |

Text values are trimmed: spaces at the start and the end are removed when you save.

## The conditions

| Condition | In the editor | Value |
|---|---|---|
| [Address](https://docs.posternmail.app/rules/conditions/#address) | **Address** | A part (From, To, ...), an operator and a text |
| [Text](https://docs.posternmail.app/rules/conditions/#text) | **Text** | Subject, message text or attachment name, an operator and a text |
| [Header](https://docs.posternmail.app/rules/conditions/#header) | **Header** | A header name, an operator and (except for present or missing) a text |
| [Attachments](https://docs.posternmail.app/rules/conditions/#attachments) | **Attachments** | **Has attachments** or **Has no attachments** |
| [Size](https://docs.posternmail.app/rules/conditions/#size) | **Size** | **Larger than** or **Smaller than** a number of KB |
| [Time received](https://docs.posternmail.app/rules/conditions/#time-received) | **Time received** | Weekdays and a time window |
| [Is a reply](https://docs.posternmail.app/rules/conditions/#is-a-reply) | **Is a reply** | **Is a reply** or **Is not a reply** |
| [Sent automatically](https://docs.posternmail.app/rules/conditions/#sent-automatically) | **Sent automatically** | **Sent automatically (newsletters, notifications)** or **Sent by a person** |

All text comparisons ignore upper and lower case.

### Address

Compares the addresses in one part of the message.

| Part | In the editor | Which addresses |
|---|---|---|
| `from` | **From** | The sender |
| `to` | **To** | Every address in To |
| `cc` | **Cc** | Every address in Cc |
| `reply_to` | **Reply-To** | Every address in Reply-To |
| `any_recipient` | **To or Cc** | Every address in To and Cc together. Bcc is not visible in received mail. |

| Operator | In the editor | Matches when |
|---|---|---|
| `contains` | **contains** | One of the addresses contains the text, in the address **or** in the display name. `acme` matches `Acme Billing <billing@example.com>`. |
| `not_contains` | **doesn't contain** | **None** of the addresses contains the text, in address or name. A part with no addresses (an empty Cc) matches. |
| `equals` | **is** | One of the addresses is exactly the text, for example `billing@acme.com`. The display name is not compared. |
| `domain` | **is at domain** | One of the addresses is at this domain **or one of its subdomains**. `acme.com` matches `a@acme.com` and `a@mail.acme.com`, not `a@notacme.com`. A leading `@` is ignored, so `@acme.com` works too. |
| `regex` | **matches pattern** | The [pattern](https://docs.posternmail.app/rules/conditions/#patterns) matches either `Name <address>` or the bare address. |

Addresses are compared as written in the message. A `+tag` is part of the address here:
**is** `you@example.com` does not match `you+shop@example.com`, but **contains** `you` does.

### Text

| Part | In the editor | What is compared |
|---|---|---|
| `subject` | **Subject** | The subject line |
| `body` | **Message text** | The plain-text part of the message. A message that only has HTML is converted to text first. |
| `attachment_name` | **Attachment name** | The file name of each attachment. Inline images without a name are not counted. |

| Operator | In the editor | Matches when |
|---|---|---|
| `contains` | **contains** | The text appears anywhere |
| `not_contains` | **doesn't contain** | The text appears nowhere. For **Attachment name**: no attachment name contains it, which includes messages without attachments. |
| `equals` | **is** | The whole value is exactly the text (spaces at the ends ignored) |
| `starts_with` | **starts with** | The value starts with the text (spaces and line breaks at the start ignored) |
| `ends_with` | **ends with** | The value ends with the text (spaces and line breaks at the end ignored) |
| `regex` | **matches pattern** | The [pattern](https://docs.posternmail.app/rules/conditions/#patterns) matches somewhere in the value |

For **Attachment name**, the condition matches when **one** of the attachments matches (except
**doesn't contain**, see above).

### Header

Compares any header of the message, such as `List-Id`, `X-Spam-Flag` or
`Authentication-Results`. The editor suggests `List-Id`, `List-Unsubscribe`, `Auto-Submitted`,
`Precedence`, `X-Spam-Flag`, `X-Mailer` and `Authentication-Results`, but you can type any name.
The name is not case sensitive (`list-id` is the same as `List-Id`) and is written without the
colon. It may contain printable ASCII characters except `:`.

| Operator | In the editor | Matches when |
|---|---|---|
| `exists` | **is present** | The message has this header (no value needed) |
| `not_exists` | **is missing** | The message does not have this header |
| `contains` | **contains** | A value of the header contains the text |
| `equals` | **is** | A value of the header is exactly the text (spaces at the ends ignored) |
| `regex` | **matches pattern** | The [pattern](https://docs.posternmail.app/rules/conditions/#patterns) matches somewhere in a value |

A header that appears several times (like `Received`) matches when one of its values matches.
`From`, `To`, `Cc`, `Reply-To`, `Subject` and `In-Reply-To` can always be used, even when the
stored copy of the message has no header block; the address headers are then compared in the
form `Name <address>`, several addresses separated by `, `.

#### Conditions that need the message text

The Hub keeps a few headers of every message without downloading its body: `From`, `To`, `Cc`,
`Reply-To`, `Subject`, `Message-Id`, `In-Reply-To`, `References`, `List-Id`,
`List-Unsubscribe`, `List-Unsubscribe-Post`, `Auto-Submitted`, `Precedence`,
`X-Auto-Response-Suppress` and `Authentication-Results`. Conditions on these, and on everything
else on this page except the two below, are decided the moment the message arrives.

These need the full message:

- **Text** with **Message text** or **Attachment name** (a message without attachments is decided
  at once for **Attachment name**),
- **Header** with any other header name.

For such a rule the Hub fetches the message body in the background and decides then, usually
within seconds. Rules that do not need the body are never held up by it. Two things follow:

- Rules below a waiting rule also wait, but only if the waiting rule could stop them (it is Live
  and has **Skip the rules below this one** or a step that takes the message away).
- A rule decided this late can no longer change whether you get a notification or which Inbox tab
  the message lands in. In the activity log, **Notify me** and **Inbox tab** steps of such a rule
  show **Skipped** with "Decided after the message was announced (the rule needed the message
  body)". Everything else runs normally.

If the body cannot be fetched (the provider is down, the mailbox needs you to sign in), the Hub
tries again after 30 seconds, 2, 10 and 30 minutes, 1, 3, 6, 12 and 12 hours. After 10 attempts
(about two days) it gives up and writes a **Failed** entry for each waiting rule: "The message
body could not be loaded: ...".

### Attachments

**Has attachments** or **Has no attachments**, as your mail provider reports it. Once the body is
known, a message with any attachment part counts as having attachments.

### Size

**Larger than** or **Smaller than** a size in KB (1 KB = 1,024 bytes), from 1 to 1,000,000. The
size is the whole message as the provider reports it, attachments included. A message of
exactly the given size matches neither.

### Time received

Matches messages received on certain weekdays within a time window.

- **Days**: pick one or more of Sun to Sat.
- **from** and **to**: times in 24-hour `HH:MM`. Both ends are included.
- If **to** is earlier than **from**, the window runs over midnight: **from** `18:00` **to**
  `08:00` means 18:00 to 23:59 and 00:00 to 08:00. The editor then says "Runs over midnight."
- The weekday is the weekday of the moment the message arrived. With Mon to Fri and 18:00 to
  08:00, a message on Saturday at 02:00 does not match, because Saturday is not picked.

The time is the time your provider received the message (for example IMAP's internal date),
or the message's `Date` header when the provider does not report one.

**Time zone.** The times are read in the time zone set under **Settings → Rules → Limits and
storage → Time zone for “Time received”**. The default, **Your Hub's time zone**, is the time
zone of the machine or container the Hub runs in.

> [!IMPORTANT]
> The Docker Compose setup runs the Hub in UTC unless you set `TZ` in `.env`. If your windows
> seem shifted by some hours, pick your time zone in the rule settings (for example
> `Europe/Berlin`). A desktop or mobile app that runs rules while the Hub is away uses its own
> time zone.

### Is a reply

**Is a reply** matches a message that has an `In-Reply-To` header or whose subject starts with
`Re:`, `AW:`, `SV:`, `Antw:` or `WG:` (any case, spaces before the colon allowed). **Is not a
reply** matches all others.

### Sent automatically

**Sent automatically (newsletters, notifications)** matches a message with at least one of:

- an `Auto-Submitted` header with any value other than `no` (out-of-office and other automatic
  replies, mail sent by programs),
- a `List-Id` header (mailing lists),
- a `List-Unsubscribe` header (newsletters and most marketing mail),
- a `Precedence` header of `bulk`, `list` or `junk`.

**Sent by a person** matches messages with none of these. All four headers are kept with every
message, so this condition is decided at once.

## Patterns

**matches pattern** uses regular expressions in the
[RE2 syntax](https://github.com/google/re2/wiki/Syntax), the syntax of Go. They are always
case insensitive and run in linear time, so a pattern cannot slow the Hub down.

- A pattern matches anywhere in the value. Use `^` and `$` to match the start and end:
  `^invoice` versus `invoice`.
- Alternatives: `invoice|receipt`. Character classes: `[0-9]{6}`. Escape dots: `acme\.com`.
- Look-ahead and look-behind (`(?=...)`, `(?<=...)`) and back-references (`\1`) are not
  supported.

The Hub checks the pattern when you save. An invalid one is refused with "Not a valid regular
expression:" and the reason, for example `missing closing )`. The editor also marks a pattern it
cannot read with "This pattern isn't valid." before you save.

| Goal | Condition |
|---|---|
| Any sender at a company and its subdomains | Address, From, is at domain, `acme.com` |
| Invoices with a number in the subject | Text, Subject, matches pattern, `invoice\s*#?\d+` |
| PDF attachments | Text, Attachment name, ends with, `.pdf` |
| Mail flagged as spam by your provider | Header, `X-Spam-Flag`, is, `YES` |
| Mail that failed DMARC | Header, `Authentication-Results`, contains, `dmarc=fail` |
| Mail outside working hours | Time received, Mon to Fri, from `18:00` to `08:00` |
| Newsletters | Sent automatically, Sent automatically (newsletters, notifications) |

## Messages from the editor

| Message | Meaning |
|---|---|
| Enter something to look for. | A text value is empty. |
| Use at most 500 characters. | A text value is too long. |
| This pattern isn't valid. | The pattern cannot be read. |
| Enter a header name. | A header condition without a name. |
| Enter a size between 1 and 1,000,000 KB. | The size is out of range. |
| Pick at least one day. | A time condition without weekdays. |
| Enter a time. | A time is missing. |
| Add a condition, or a search under Advanced. | The rule has neither. |
| Use at most 20 conditions. / Use at most 20 exceptions. | Too many rows. |
| This needs the message text, so the rule may run a little later. | A hint, not an error: see [above](https://docs.posternmail.app/rules/conditions/#conditions-that-need-the-message-text). |
