WIP: sending method as an explicit choice, kept in payload-kv (superseded: the choice moves to a stored field)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 22:30:03 +01:00
co-authored by Claude Fable 5.2
parent 68a2555277
commit c6841a1674
34 changed files with 740 additions and 96 deletions
+60 -21
View File
@@ -1,19 +1,19 @@
# email
Mail works with **no setup**: a site with no SMTP server in the panel sends through the
Mail works with **no setup**: a new site sends through the
**Intecion Notifications Webservice** — **IWSN** for short, the name the admin panel uses (constant
`RELAY_SHORT_NAME`); below also "the mail service" — from
`[email protected]`, under the site's own sender name. Two more transports exist:
| Transport | When | Sender address |
|---|---|---|
| **The mail service** (`relayAdapter`, inside `mailAdapter()`) | the default — nothing filled in | `[email protected]`; replies go to the panel's "Sender address" |
| **SMTP from the panel** (`panelSmtpAdapter`) | on the client's request: its own mail server, its own sender address | the panel's "Sender address" |
| **Microsoft Graph** (`graphAdapter`) | **deprecated since 1.17.0, removed in 2.0** — sites that have the `GRAPH_*` variables | `GRAPH_SENDER` |
| **The mail service** (`relayAdapter`, inside `mailAdapter()`) | the default — "Sending method": **IWSN (built-in)** | `[email protected]`; replies go to the panel's "Sender address" |
| **SMTP from the panel** (`panelSmtpAdapter`) | "Sending method": **Own SMTP server** — on the client's request: its own mail server, its own sender address | the panel's "Sender address" |
| **Microsoft Graph** (`graphAdapter`) | "Sending method": **Microsoft Graph (deprecated)** — **deprecated since 1.17.0, removed in 2.0**; needs the `GRAPH_*` variables on the server | `GRAPH_SENDER` |
All three implement Payload's `PayloadEmailAdapter`, so `payload.sendEmail`, form mail, password reset and
account verification work whatever the transport. The project wires **one** adapter,
`buildConfig({ email: mailAdapter() })`; which transport a send takes is decided on every send.
`buildConfig({ email: mailAdapter() })`; which transport a send takes is the admin's choice in the panel.
A separate `sendEmail` helper (from `/server`) sends **always through the SMTP** from the panel.
## Wiring
@@ -33,26 +33,64 @@ The plugin does **not** set `config.email` — without `email:` Payload uses a m
## Which transport a send takes
Decided on every send from Site integrations → E-mail sending, with no stored value of its own
**The one an admin chose** in Site integrations → E-mail sending → **"Sending method"** (since 1.17.1): "IWSN
(built-in)", "Own SMTP server" or "Microsoft Graph (deprecated)". What is chosen is what is used, and it is shown:
the state block names it, the test e-mail names it, and only the fields of the chosen method are on the page (the
SMTP server, port, user name and password under "Own SMTP server"; the service's state block under "IWSN").
- **No silent switch.** Which fields happen to be filled in, or which variables the server has, no longer picks
the method. "IWSN" chosen with an SMTP server filled in sends through the service; "Own SMTP server" chosen
never contacts the service.
- **A chosen method that cannot be used fails loudly, and nothing is sent another way:**
| Chosen | What is wrong | A send | Where it shows |
|---|---|---|---|
| Own SMTP server | server, user name or password missing | `{ sent: false, code: 'not-configured' }` | state block: "Your SMTP server is chosen, but its settings are incomplete…"; status `smtp-incomplete`; doctor `mail-method` (error) |
| Microsoft Graph | the `GRAPH_*` variables are not on this server | `{ sent: false, code: 'graph-not-set-up' }` — SMTP is **not** tried | state block: "Microsoft Graph is chosen, but it is not set up on this server…"; status `graph-not-set-up`; doctor `mail-method` (error) |
| IWSN | the service cannot call the site back, the site is disconnected, waits for approval, is a held copy … | `relay-refused`, `relay-pending`, … (below) | the service's state block; doctor `mail-relay-state` |
A form submission is stored whatever happens to its e-mail, as before.
- One exception that was always there: with **Microsoft Graph** chosen, a file too large for Graph goes over the
panel's SMTP server when one is filled in ("Attachments" below).
- `mail: { relay: false }` takes the option "IWSN" away; a choice of it stored earlier then counts as no choice.
**Where the choice is stored.** In Payload's key-value store (`payload-kv`, key `MAIL_METHOD_KV_KEY`), not in a
column: the panel's field `emailMethod` is **virtual**. So **no SQL schema changes and no migration exists** —
a third value in the stored select `emailTransport` (`smtp` / `graph` since 1.6) would have changed the Postgres
enum, and could still not tell "SMTP was chosen" from the `smtp` every site has stored since before there was a
choice. `emailTransport` stays, hidden in the panel, and follows the choice where it can say it (`graph`, else
`smtp`). From code: `getMailMethod(payload)` (`'relay' | 'smtp' | 'graph' | null`), `setMailMethod(payload, method)`
(a seed or a deploy script; `null` = nobody has chosen), `payload.updateGlobal({ slug: 'site-integrations', data: {
emailMethod: 'smtp' } })`, `resolveMailMethod(settings, { chosen, graphEnv, relay })` →
`{ method, explicit, problem? }` (types `MailMethod`, `ResolvedMailMethod`, `MailMethodProblem`; `MAIL_METHODS`,
`isMailMethod`).
**Until somebody chooses — a site upgraded from 1.17.0 or older, and a new site.** Nothing is written on upgrade.
The method follows the order of 1.17.0, the field shows it **as selected**, and mail keeps leaving the way it did
(`resolveMailTransport(settings, { graphEnv, relay })` returns `'graph' | 'smtp' | 'relay' | 'none'`, type `MailTransport`):
1. "Sending method" is Microsoft Graph **and** the four `GRAPH_*` variables are set → **Graph**, as before (deprecated).
1. `emailTransport` is `graph` **and** the four `GRAPH_*` variables are set → **Graph** (deprecated).
2. The panel has an SMTP server (server, user name and password) → **SMTP**.
3. The four `GRAPH_*` variables are set → **Graph** (deprecated), until they are removed.
3. The four `GRAPH_*` variables are set → **Graph** (deprecated).
4. Otherwise → **the mail service**, unless it is switched off (`mail: { relay: false }`).
5. Otherwise nothing is configured: the send reports `{ sent: false, code: 'not-configured' }`, as before 1.17.0.
5. Otherwise nothing is configured: the send reports `{ sent: false, code: 'not-configured' }`.
**Half an SMTP set-up** (a server and a user name without a password, say) is not an SMTP server: until 1.16 such
a site refused to send; now its mail goes through the mail service. The server log says so once and the state
block shows "The SMTP settings below are incomplete".
| The site before the upgrade | "Sending method" shows | Mail |
|---|---|---|
| 1.17.0, no SMTP server (sends through the service) | IWSN (built-in) | as before |
| SMTP server filled in (1.16.x or 1.17.0) | Own SMTP server | as before |
| `GRAPH_*` on the server (with "Microsoft Graph" stored, or with no SMTP server) | Microsoft Graph (deprecated) | as before, with the deprecation notice |
| half an SMTP set-up, no `GRAPH_*` | IWSN (built-in), with the hint "The SMTP settings below are incomplete" | through the service, as in 1.17.0 |
| a new site | IWSN (built-in) | through the service |
**The first save of Site integrations stores what the form shows** (the form always sends the field), and from
then on the method is fixed. The state block says "Nobody has chosen the sending method yet, so the site picked
this one. Save this page to keep it." until then, and the doctor's deep run notes it (`mail-method`, info). A script
of before 1.17.1 that writes `emailTransport` alone still means what it meant: it puts the site back on the order
above (write `emailMethod` to choose).
- **A site with SMTP filled in never contacts the mail service.** Nothing changes for it.
- **SMTP on the client's request:** an admin fills in "Own SMTP server", user name and password; from the next send
the site uses the client's server and the client's own sender address. Emptying the server returns the site to
the mail service.
- There is **no fallback** from a failing SMTP server to the mail service: a client who asked for its own server
gets no mail from another address.
- "Sending method" keeps its two stored values (`smtp`, shown as "Automatic", and `graph`); no field was added.
## The mail service
@@ -466,8 +504,9 @@ expect(relay.outbox[0]).toMatchObject({ kind: 'system', to: ['[email protected]'] })
## SMTP — on the client's request
The panel's SMTP fields (`smtpHost`, `smtpPort` (default 587), `smtpUser`, `smtpPassword`, `smtpFromAddress`,
`smtpFromName`) and `emailTransport` live in Site integrations (E-mail sending tab). Filling in server, user name
and password switches the site to the client's server.
`smtpFromName`) live in Site integrations (E-mail sending tab); server, port, user name and password are shown
while "Sending method" is "Own SMTP server". Choose that method, fill them in and save: the site sends through the
client's server (a site nobody has chosen a method for yet does so as soon as the three are filled in).
- **The password is write-only and stays with its server**: the panel shows "A value is set"; changing the
server, port or user name asks for the password again ([security.md](./security.md) "Stored secrets").
@@ -587,8 +626,8 @@ To the mail service (when the owner says so):
1. Upgrade; check that `serverURL` is the site's public address.
2. Tell Intecion the host: while the service runs with enrolment "pending" (from its launch until Intecion switches to open enrolment), every new site waits for approval; the panel says so. Under open enrolment the site is active as soon as it enrols.
3. Remove the four `GRAPH_*` variables in Coolify and redeploy. If "Sending method" says Microsoft Graph, set it
to "Automatic".
3. Site integrations → E-mail sending → "Sending method": choose **IWSN (built-in)** and save. Then remove the four
`GRAPH_*` variables in Coolify and redeploy.
4. Site integrations → E-mail sending → send a test e-mail: the answer says "through IWSN".
5. Check "Sender name" and "Sender address" (now the reply address), and tell the client that mail now comes from
`notification@webservices.intecion.net` instead of the present mailbox (allow-lists, filters).