# How Postern works

> The parts of Postern (the Hub, the apps, the optional push relay), who keeps which data, and what happens when mail arrives, when you send, and when the Hub is down.

Source: https://docs.posternmail.app/how-it-works/

Postern has two parts: one small server program you run, the **Hub**, and the **apps** that
sign in to it. A third, optional part, the **push relay**, carries notifications to phones.
This page explains what each part does and where your data lives, so the rest of the docs make
sense.

Postern is not a mail server. It hosts no addresses and receives no mail over SMTP. Your mail
stays at Gmail, Outlook, iCloud, Fastmail or your own mail server, and Postern connects to
those mailboxes like any mail client does.

```text
   Your devices: real mail clients               Your server (Linux box, NAS, Pi, VPS)
┌───────────────────────────────┐             ┌──────────────────────────────────────────┐
│ Desktop / Mobile app          │  account,   │            Hub (one Go binary)           │
│  ┌─────────────────────────┐  │  settings,  │  accounts · devices · credentials vault  │
│  │ mail engine (same Go    │◀─┼─────────────┤  settings · rules · tags · drafts · marks│
│  │ code as the Hub)        │  │  sign-ins,  │  inbox watcher (instant IMAP IDLE)       │
│  │ local copy of your mail │  │  snoozes…   │  push · send later · snooze wake-ups     │
│  └───────────┬─────────────┘  │             │  web app (+ full mail copy while used)   │
│ Web app ─────┼────────────────┼────────────▶└───────────────┬──────────────────────────┘
└──────────────┼────────────────┘                             │ watches inboxes only
               │ connects directly                            │ (no copy of your mail)
               ▼                                              ▼
     Gmail · Outlook · iCloud · Yahoo · Fastmail · any IMAP/SMTP (or JMAP)
```

## The Hub

The Hub is one program, `postern-hub`, with one data folder and one master key. You run it on
a machine that stays on: a home server, a NAS, a Raspberry Pi or a VPS. It is the **control
plane** of your setup:

- **Your account.** You sign in to the Hub with a password plus a second factor, a passkey or
  single sign-on. One Hub can hold several people, each with their own mailboxes, settings
  and encryption key.
- **Your mailbox sign-ins.** You connect each mailbox once, on the Hub. The passwords and
  tokens are encrypted at rest and handed only to devices you approved.
- **Everything you set up.** Spaces, identities, signatures, tags, rules, templates, drafts,
  settings, and the state of each conversation (snoozed, pinned, muted, follow-up) live on the
  Hub and sync to every device.
- **The work that must happen while your devices are off.** The Hub watches each inbox with
  an instant IMAP IDLE connection, sends push notifications, runs your rules, wakes snoozed
  conversations, reminds you of follow-ups and sends scheduled mail.
- **The web app.** The Hub serves the web app at its own address. A browser cannot talk to
  mail servers, so the web app uses the Hub's mail engine. While you use it, the Hub keeps a
  full copy of your mail.

The Hub listens on `127.0.0.1:8080` by default, so only the machine itself can reach it. You
decide how your devices reach it: [Reaching your Hub](https://docs.posternmail.app/reach/).

## The apps

| App | How it gets your mail | Without the Hub |
|---|---|---|
| **Web** | Through the Hub's engine | Does not work |
| **Desktop** (macOS, Windows, Linux) | Directly from your providers, with its own copy on the computer | Keeps working, including sending |
| **iOS and Android** | Directly from your providers, with its own copy on the phone | Keeps working, including sending |

The desktop and phone apps run the **same mail engine as the Hub**, compiled into the app.
They get the mailbox sign-ins from the Hub, then talk to Gmail, Outlook or your IMAP server
themselves. This is called **direct mode**. Everything that is not mail itself (settings,
rules, drafts, snoozes) still goes through the Hub, so every device stays in sync. See
[Apps](https://docs.posternmail.app/apps/) and [Offline behaviour](https://docs.posternmail.app/apps/offline/).

New devices sign in with **pairing**: a device that is already signed in shows a QR code, the
new one scans it, and you approve it. See [Pairing a device](https://docs.posternmail.app/apps/pairing/).

## The push relay

Apple and Google only deliver push notifications that are signed with the app developer's
keys. A Hub you run cannot hold those keys, so it sends a short "new mail" message to a
**push relay**, which forwards it to Apple or Google. The relay is optional; without it, phones
catch up when you open the app. With previews on, the message carries the sender, the subject
and the first line; with previews off it carries no mail content. See [How push works](https://docs.posternmail.app/relay/)
and [What the relay sees](https://docs.posternmail.app/relay/what-it-sees/).

## What happens when

**New mail arrives.** The provider's IMAP IDLE connection tells the Hub within seconds. The Hub
runs your rules on it, then sends a push notification and tells every signed-in app over a
WebSocket. Desktop and phone apps fetch the message from the provider themselves.

**You send a message.** A desktop or phone app sends it itself, through your provider, after
the undo send window. A message scheduled with send later is handed to the Hub, which sends it
on time even when every device is off. The web app sends through the Hub.

**The Hub is down.** Desktop and phone apps keep reading and sending mail. Changes to settings,
snoozes and drafts wait on the device and sync when the Hub is back. The web app, push
notifications, rules and scheduled sends wait for the Hub.

**A device is lost.** Revoke it in **Settings → Devices** on any other device. Its session ends,
and the app deletes the mail it kept for that Hub the next time it reaches the Hub.

## Where your data lives

| Data | Where |
|---|---|
| Your mail | At your provider. A copy on each desktop and phone app. A full copy on the Hub only while you use the web app (or always, if you turn that on). |
| Mailbox passwords and tokens | Encrypted in the Hub's database. Sealed copies on devices you approved. |
| Settings, rules, tags, drafts, snoozes | In the Hub's database, mirrored to each device. |
| The master key | One file next to the data folder (not inside it). Back it up separately. |
| Backups | In the Hub's data folder by default, every 24 hours. |

Details: [Encryption at rest](https://docs.posternmail.app/security/encryption/) and [Backups and
restore](admin/backups.md).

## What Postern does not do

- **Host your mail.** You need a mailbox at a provider or on your own mail server.
- **Run as a service for you.** There is no hosted Postern; you run the Hub.
- **Phone home.** The Hub and the apps have no telemetry. Their only connections are to your
  mail providers, your Hub and, if you use push, the relay.

The full design, for developers, is in
[docs/dev/DIRECT-MODE.md](https://github.com/TheOriUHD/postern/blob/main/docs/dev/DIRECT-MODE.md).
