E-mail: "Sending method" is an explicit choice - IWSN (built-in), Own SMTP server, Microsoft Graph (deprecated)

What is chosen is what is used. The choice is the stored field `emailMethod` of Site integrations (empty until
somebody chooses); `emailTransport` of 1.6 - 1.17.0 stays, hidden.

- no silent switch: a chosen method that cannot be used (SMTP incomplete, Graph without GRAPH_*) fails loudly
  - status smtp-incomplete / graph-not-set-up, state block, doctor mail-method - and nothing is sent another way
- upgrade writes nothing and loses no mail: until somebody chooses, the order of 1.17.0 applies and the field
  shows its result as selected; the first save of the panel's form stores the choice
- only the chosen method's fields are shown; the test e-mail names the method (Graph too)
- mail: { relay: false } does not offer "IWSN" (the stored values stay three: one SQL enum for every project)
- Postgres / SQLite: one migration (a column). Before it runs the plugin's readers read the global without the
  field, mail goes the old way, one loud log line and the doctor say what to do
- getMailMethod, setMailMethod, resolveMailMethod, readMailSettings, mailMethodColumnMissing exported
- tests on SQLite, Postgres and MongoDB, with and without the migration; docs, AGENT.md, skills, changelog

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 22:39:03 +01:00
co-authored by Claude Fable 5.2
parent c6841a1674
commit ee8ea42956
26 changed files with 376 additions and 138 deletions
+23 -31
View File
@@ -3,7 +3,7 @@ import type { Field, GlobalConfig } from 'payload'
import { ValidationError } from 'payload'
import { userMeetsRole } from '../../modules/access/index.js'
import { isMailMethod, setMailMethod } from '../../modules/email/method.js'
import { isMailMethod } from '../../modules/email/method.js'
import { INTEGRATION_SECRET_FIELDS, maskSecretsOnRestore, SMTP_PASSWORD_BOUND_TO, writeOnlySecrets } from '../../modules/secrets/writeOnly.js'
import { indexNowFields } from '../../modules/seo/indexNow.js'
import { analyticsFields } from './fields/analytics.js'
@@ -90,45 +90,37 @@ export function buildSiteIntegrations({
hooks: {
// a restore answers with the version document: the secrets are masked there too
...(writeOnly ? { afterRead: [maskSecretsOnRestore] } : {}),
afterChange: [
async ({ context, doc, req }) => {
if (isMailMethod(context?.iaplMailMethod)) {
await setMailMethod(req.payload, context.iaplMailMethod)
// (the answer was read before this write: it must show what was just chosen)
if (doc && typeof doc === 'object') {(doc as Record<string, unknown>).emailMethod = context.iaplMailMethod}
} else if (context?.iaplMailMethod === null) {
await setMailMethod(req.payload, null)
}
return doc
},
],
// What the caller sent for the sending method, before Payload merges the stored document into the data.
beforeChange: [
// Keys edited in the panel → clear that integration's old status, so a stale
// "not working" banner doesn't linger until the next form submission.
async ({ context, data, originalDoc, req }) => {
({ context, data, originalDoc, req }) => {
if (context?.ipalIntegrationStatus || !data) {return data}
const changed = (k: string) => k in data && data[k] !== originalDoc?.[k]
const cleared = { checkedAt: null, code: null, message: null, since: null, state: null }
// "Sending method" (the virtual field `emailMethod`): the choice is written to the key-value store
// once the global is saved (afterChange). The stored `emailTransport` follows where it can say it.
// (`data` is merged with the stored document by now; what the caller really sent was noted before the
// merge — beforeOperation below. Without that note: a value that differs from the stored one was sent.)
const noted = context?.iaplMailSent as { method?: unknown; transport?: boolean } | undefined
const sent = noted ? noted.method : data.emailMethod !== originalDoc?.emailMethod ? data.emailMethod : undefined
// (a virtual field's value is not checked against its options by Payload)
if (sent !== undefined && sent !== null && !(isMailMethod(sent) && (sent !== 'relay' || mailRelay))) {
throw new ValidationError({ errors: [{ message: 'This is not a sending method.', path: 'emailMethod' }], global: 'site-integrations' }, req.t)
}
if (isMailMethod(sent)) {
data.emailMethod = sent
const before = await shownMailMethod(req.payload, (originalDoc ?? {}) as never, mailRelay).catch(() => null)
if (before !== data.emailMethod) {data.emailStatus = cleared}
context.iaplMailMethod = data.emailMethod
// "Sending method" (`emailMethod`). `data` is merged with the document as it was READ by now, and that read
// shows the method in effect as selected — which must not be stored as a choice nobody made. So: what the
// caller really sent was noted before the merge (beforeOperation below; without that note, a value that
// differs from the one read was sent), and what is stored was noted by the field's own read.
const noted = context?.iaplMailSent as { method?: unknown; sent?: boolean; transport?: boolean } | undefined
const sent = noted ? noted.sent : data.emailMethod !== originalDoc?.emailMethod
const stored = isMailMethod(context?.iaplMailMethodStored) ? context.iaplMailMethodStored : null
if (sent && isMailMethod(data.emailMethod)) {
if (data.emailMethod === 'relay' && !mailRelay) {
throw new ValidationError({ errors: [{ message: 'This sending method is switched off for this site.', path: 'emailMethod' }], global: 'site-integrations' }, req.t)
}
if (shownMailMethod({ ...(originalDoc ?? {}), emailMethod: stored } as never, mailRelay) !== data.emailMethod) {data.emailStatus = cleared}
// the stored field of 1.6 – 1.17.0 follows where it can say it
data.emailTransport = data.emailMethod === 'graph' ? 'graph' : 'smtp'
} else if (sent && data.emailMethod !== null && data.emailMethod !== undefined) {
// not a method: left as it is — the field's own validation refuses it
} else if (sent) {
data.emailMethod = null // un-chosen on purpose (`setMailMethod(payload, null)`): the order of 1.17.0 again
} else if ((noted ? noted.transport : true) && changed('emailTransport')) {
// written the old way (a script from before 1.17.1): it means what it meant — the order of 1.17.0
context.iaplMailMethod = null
data.emailMethod = null
} else {
data.emailMethod = stored
}
if (changed('turnstileSecretKey') || changed('turnstileSiteKey')) {data.turnstileStatus = cleared}
if (Object.keys(data).some((k) => (k === 'emailTransport' || k.startsWith('smtp')) && changed(k))) {
@@ -141,7 +133,7 @@ export function buildSiteIntegrations({
({ args, context, operation }) => {
const data = (args as { data?: Record<string, unknown> } | undefined)?.data
if (operation === 'update' && data && typeof data === 'object' && context) {
context.iaplMailSent = { method: data.emailMethod ?? undefined, transport: 'emailTransport' in data }
context.iaplMailSent = { sent: 'emailMethod' in data, transport: 'emailTransport' in data }
}
return args
},