Account mail after commit: matched by its token, not by the address (P-4)

holdAccountMailFor bound the next message to an address to whichever
request held it: two resets for one account at once could lose the first
one's mail on the second's rollback, and a form's mail to that address was
deferred to the reset's transaction.

Payload calls generateEmailSubject({ req, token, user }) right before it
sends the reset or verify mail, and the mail carries that token. The plugin
now wraps that function on every auth collection (a project's own one is
kept, Payload's subject otherwise) and holds the token for the request's
transaction; mailAdapter defers only a message to that account alone whose
body contains a held token. An AsyncLocalStorage mark set in a
beforeOperation hook was not used: Payload awaits the hook, and a mark set
inside it does not reach the send (relay/kinds.ts says the same).

Building blocks renamed with it: holdAccountMail(token, address, req),
releaseAccountMail(token). Both review rows are tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 01:36:17 +01:00
co-authored by Claude Opus 5.5
parent 4efadb21d5
commit 919953e65e
8 changed files with 151 additions and 67 deletions
+4 -3
View File
@@ -146,10 +146,11 @@ once there are more than 20.
`inWatchedTransaction`, `transactionsBusy`, `transactionWatchState`, `poolSize`, `defaultConnectionReserve`,
`RESERVE_ESCAPE_MS` ([deployment.md](./deployment.md) §4.3a, with the measured table).
- **Payload's "forgot password" and "verify your e-mail" go out after their transaction commits** when sent through
`mailAdapter()` (`mail.accountMailAfterCommit`, default on; same request, nothing sent on a rollback). With the mail
`mailAdapter()` (`mail.accountMailAfterCommit`, default on; same request, nothing sent on a rollback). The message is
matched to its request by its token (Payload's `generateEmailSubject` is wrapped; a project's own one is kept). With the mail
service hanging, 30 resets and 30 submissions at once finish in about one send budget (11 s) instead of 43.5 s.
Building blocks `withAccountMailAfterCommit`, `sendAccountMailAfterCommit`, `holdAccountMailFor`,
`releaseAccountMailFor`.
Building blocks `withAccountMailAfterCommit`, `sendAccountMailAfterCommit`, `holdAccountMail`,
`releaseAccountMail`.
- **Form e-mails go out after the submission's transaction commits** — same request, same answer to the visitor
(a file that could not be sent is still reported). A submission rolled back by a later hook now sends nothing; a
send that fails after the commit leaves the submission stored (as a failed e-mail always did). Sites without
+1 -1
View File
@@ -1247,7 +1247,7 @@ add missing hosts with flags / `extra`; (3) change `mode` to `'enforce'` in both
| Every message says what it is (form notification, form confirmation, account, test, system) | — (other transports ignore the mark) | `withMailKind`, `runWithMailKind`, `expectMailTo`, `mail.relay.kind`, `buildFormMailKindHook` |
| Microsoft Graph as transport: deprecated, one warning per start, removed in 2.0 | `mail: { graphDeprecationWarning: false }` (the warning only) | move off it: email.md "Moving off Microsoft Graph" |
| Postgres: a fifth of the pool (at least 2) stays free of transactions — at most `pool size − reserve` open at once, the next waits; a transaction begun inside another one (a hook without `req`) takes no place and goes at once, but never the last connection. Without it ten form submissions or "forgot password" requests at once stopped the site | `connectionReserve: false` | `connectionReserve: <number>`; `reserveConnections(payload, n)` in your own `onInit` (without `iapl()`); `watchTransactions`, `afterCommit`, `inWatchedTransaction`, `transactionsBusy`, `transactionWatchState` (`{ active, nested, waiting, reserve }`), `poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS` (deployment.md §4.3a) |
| Payload's "forgot password" / "verify your e-mail" through `mailAdapter()` go out right after their transaction commits (same request); a rolled-back operation sends nothing | `mail: { accountMailAfterCommit: false }` | your own adapter: `sendAccountMailAfterCommit(message, send)`; `withAccountMailAfterCommit(collection)`, `holdAccountMailFor`, `releaseAccountMailFor` (email.md "Payload system mail") |
| Payload's "forgot password" / "verify your e-mail" through `mailAdapter()` go out right after their transaction commits (same request); a rolled-back operation sends nothing | `mail: { accountMailAfterCommit: false }` | your own adapter: `sendAccountMailAfterCommit(message, send)`; `withAccountMailAfterCommit(collection)`, `holdAccountMail`, `releaseAccountMail` (email.md "Payload system mail") |
| Form e-mails go out right after the submission's transaction commits (same request, same answer); a rolled-back submission sends nothing; a send that fails after the commit leaves the submission stored. No transactions (MongoDB without a replica set, SQLite): at once, as before | `forms: { emailsInTransaction: true }` | `sendAfterCommit(prepare, deliver)`, `sendFormEmails(req, emails)`, `afterCommit(req, fn)` (forms.md "When e-mails go out") |
| SMTP time-outs: 10 s to connect, 30 s greeting, 60 s silence | — | `mailAdapter({ smtp: { timeouts: { connectionMs, greetingMs, socketMs } } })`, `panelSmtpAdapter({ timeouts })`; `SMTP_TIMEOUT_DEFAULTS`, type `SmtpTimeouts` (email.md "SMTP") |
+4 -3
View File
@@ -398,9 +398,10 @@ Without `iapl()` (building blocks only): `onInit: (payload) => { reserveConnecti
Building blocks: `reserveConnections`, `watchTransactions`, `afterCommit` (run work right after a transaction
commits), `inWatchedTransaction`, `transactionsBusy`, `transactionWatchState` (for a health or debug route:
`{ active, nested, waiting, reserve }`), `poolSize`, `defaultConnectionReserve`, `RESERVE_ESCAPE_MS`; for account mail
`withAccountMailAfterCommit(collection)` (the hook the plugin puts on every auth collection),
`sendAccountMailAfterCommit(message, send)` (what an e-mail adapter of your own calls first), `holdAccountMailFor`,
`releaseAccountMailFor`. Still true: a hook that writes without `req` is a transaction of its own — it is not rolled
`withAccountMailAfterCommit(collection)` (what the plugin does to every auth collection: wraps its
`generateEmailSubject`, which Payload calls with the request and the token right before the send),
`sendAccountMailAfterCommit(message, send)` (what an e-mail adapter of your own calls first), `holdAccountMail`,
`releaseAccountMail`. Still true: a hook that writes without `req` is a transaction of its own — it is not rolled
back with its caller's. Pass `req`.
**Check** (staging, Postgres): send 30 submissions of a form at once (or 30 "forgot password" requests for different
+4 -1
View File
@@ -498,7 +498,10 @@ Password reset and e-mail verification go through Payload's built-in mechanism (
account mail ("What a message is"). Payload sends them inside the operation's database transaction; `mailAdapter()`
sends them right **after** it commits instead (still in the same request; nothing when it is rolled back), so a slow
mail server holds no database connection — `mail: { accountMailAfterCommit: false }` keeps them inside
([deployment.md](./deployment.md) §4.3a). An e-mail adapter of your own does the same by calling
([deployment.md](./deployment.md) §4.3a). The message is matched to its request by the token it carries (and the
account's address), never by the address alone: a form's mail to the same address goes out as usual. A project's own
`forgotPassword.generateEmailSubject` / `verify.generateEmailSubject` is kept; an own `generateEmailHTML` must put the
token in the body (any working reset link does), or that mail is sent inside the transaction as before. An e-mail adapter of your own does the same by calling
`sendAccountMailAfterCommit(message, () => send(message))` first.
## Masking secrets in the panel (MaskedField)