Sending method, the owner's answers: nothing changes until another option is picked; the panel warns before the save

- a save that sends the method already shown, while nothing is stored, stores nothing: the site keeps sending
  exactly as before and keeps following what is filled in, until somebody picks ANOTHER option
- the state block reads the form as it is edited and warns at once when "Own SMTP server" is picked with server,
  user name or password empty, or "Microsoft Graph" on a server without GRAPH_* (graphReady in the panel state);
  it reloads after every save
- texts, doctor's info finding, docs, skills, changelog; reference e2e checks the warning

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 23:15:42 +01:00
co-authored by Claude Fable 5.2
parent 8c19391590
commit dc3e594d38
17 changed files with 107 additions and 33 deletions
@@ -1,15 +1,17 @@
'use client'
import { useConfig, useTranslation } from '@payloadcms/ui'
import { useConfig, useFormFields, useTranslation } from '@payloadcms/ui'
import { useCallback, useEffect, useState } from 'react'
import { formatDateTime, iaplT } from '../../../modules/admin-i18n/t.js'
import { RELAY_CONNECT_PATH, RELAY_DISCONNECT_PATH, RELAY_STATUS_PATH } from '../../../modules/email/relay/constants.js'
import { SECRET_CLEAR } from '../../../modules/secrets/constants.js'
type Window = { limit: number; remaining: number }
type State = {
accessKeyField?: boolean
error?: string
graphReady?: boolean
message?: string
methodChosen?: boolean
methodProblem?: 'graph-not-set-up' | 'smtp-incomplete'
@@ -76,9 +78,18 @@ export const MailRelayStatus = () => {
[api, t],
)
// the form as it is now — before it is saved: a method picked that cannot send is said at once
const formMethod = useFormFields(([f]) => f.emailMethod?.value) as string | undefined
const formHost = useFormFields(([f]) => f.smtpHost?.value) as string | undefined
const formUser = useFormFields(([f]) => f.smtpUser?.value) as string | undefined
// (a stored password arrives as the mask: set; "Remove the stored value" is SECRET_CLEAR: not set)
const formPassword = useFormFields(([f]) => f.smtpPassword?.value) as string | undefined
const savedAt = useFormFields(([f]) => f.updatedAt?.value) as string | undefined
// on load, and again after every save of the page: the state block says what the saved settings do
useEffect(() => {
void call(RELAY_STATUS_PATH)
}, [call])
}, [call, savedAt])
const post = (path: string, body?: object) =>
call(path, { body: body ? JSON.stringify(body) : undefined, headers: body ? { 'Content-Type': 'application/json' } : undefined, method: 'POST' }, true)
@@ -106,8 +117,13 @@ export const MailRelayStatus = () => {
: state.transport === 'graph' ? text('mailRelayTransportGraph')
: text(relay ? 'mailRelayTransportNone' : 'mailRelayOff')}
</p>
{formMethod === 'smtp' && !(formHost?.trim() && formUser?.trim() && formPassword && formPassword !== SECRET_CLEAR) && !(state.methodProblem === 'smtp-incomplete' && state.transport === 'smtp') ? (
<p data-mail-method-warning="smtp" role="alert" style={warn}>{text('mailMethodSmtpIncompleteForm')}</p>
) : formMethod === 'graph' && state.graphReady === false && state.methodProblem !== 'graph-not-set-up' ? (
<p data-mail-method-warning="graph" role="alert" style={warn}>{text('mailMethodGraphNotSetUpForm')}</p>
) : null}
{state.methodProblem ? (
<p role="alert" style={warn}>{text(state.methodProblem === 'graph-not-set-up' ? 'mailMethodGraphNotSetUp' : 'mailMethodSmtpIncomplete')}</p>
<p data-mail-method-problem={state.methodProblem} role="alert" style={warn}>{text(state.methodProblem === 'graph-not-set-up' ? 'mailMethodGraphNotSetUp' : 'mailMethodSmtpIncomplete')}</p>
) : state.methodChosen === false && state.transport !== 'none' ? (
<p style={{ ...line, fontSize: '0.85rem', opacity: 0.75 }}>{text('mailMethodNotChosen')}</p>
) : null}
+7 -2
View File
@@ -105,12 +105,17 @@ export function buildSiteIntegrations({
const refuse = (message: string): never => {
throw new ValidationError({ errors: [{ message, path: 'emailMethod' }], global: 'site-integrations' }, req.t)
}
if (sent && data.emailMethod !== null && data.emailMethod !== undefined) {
// the owner's rule: a site keeps sending the way it does now until somebody picks ANOTHER option — a save that
// sends the method already shown, while nothing is stored, stores nothing
const shown = shownMailMethod({ ...(originalDoc ?? {}), emailMethod: stored } as never, mailRelay)
if (sent && stored === null && data.emailMethod === shown) {
data.emailMethod = null
} else if (sent && data.emailMethod !== null && data.emailMethod !== undefined) {
// anything but one of the three values (a number, a list, another word) is refused here: the database
// would answer with an error of its own, or take it
if (!isMailMethod(data.emailMethod)) {return refuse('This is not a sending method.')}
if (data.emailMethod === 'relay' && !mailRelay) {return refuse('This sending method is switched off for this site.')}
if (shownMailMethod({ ...(originalDoc ?? {}), emailMethod: stored } as never, mailRelay) !== data.emailMethod) {
if (shown !== data.emailMethod) {
data.emailStatus = { checkedAt: null, code: null, message: null, since: null, state: null }
}
// the stored field of 1.6 – 1.17.0 follows where it can say it
+6 -2
View File
@@ -12,8 +12,10 @@ export const integrationsTexts = defineTexts({
// Intecion mail service: the state block on the e-mail tab (MailRelayStatus) and its routes
mailMethodGraphNotSetUp: 'Microsoft Graph is chosen, but it is not set up on this server. E-mails are not sent. Ask your administrator.',
mailMethodNotChosen: 'Nobody has chosen the sending method yet, so the site picked this one. Save this page to keep it.',
mailMethodGraphNotSetUpForm: 'Microsoft Graph is not set up on this server. If you save this, e-mails will not be sent.',
mailMethodNotChosen: 'The site sends the way it did before. It changes only when you pick another sending method.',
mailMethodSmtpIncomplete: 'Your SMTP server is chosen, but its settings are incomplete. E-mails are not sent. Fill in server, user name and password.',
mailMethodSmtpIncompleteForm: 'Fill in server, user name and password below. If you save without them, e-mails will not be sent.',
mailRelayCheck: 'Check now',
mailRelayConnect: 'Connect again',
mailRelayConnected: `The site is connected to ${IWSN}.`,
@@ -138,8 +140,10 @@ export const integrationsTexts = defineTexts({
alertsUnknownError: 'Nieznany błąd.',
mailMethodGraphNotSetUp: 'Wybrano Microsoft Graph, ale nie jest on skonfigurowany na tym serwerze. E-maile nie są wysyłane. Zapytaj administratora.',
mailMethodNotChosen: 'Nikt jeszcze nie wybrał sposobu wysyłki, więc witryna dobrała ten. Zapisz tę stronę, aby go zachować.',
mailMethodGraphNotSetUpForm: 'Microsoft Graph nie jest skonfigurowany na tym serwerze. Jeśli to zapiszesz, e-maile nie będą wysyłane.',
mailMethodNotChosen: 'Witryna wysyła tak jak dotąd. Zmieni to dopiero wybór innego sposobu wysyłki.',
mailMethodSmtpIncomplete: 'Wybrano Twój serwer SMTP, ale jego ustawienia są niepełne. E-maile nie są wysyłane. Uzupełnij serwer, nazwę użytkownika i hasło.',
mailMethodSmtpIncompleteForm: 'Uzupełnij poniżej serwer, nazwę użytkownika i hasło. Jeśli zapiszesz bez nich, e-maile nie będą wysyłane.',
mailRelayCheck: 'Sprawdź teraz',
mailRelayConnect: 'Połącz ponownie',
mailRelayConnected: `Witryna jest połączona z ${IWSN}.`,
+1 -1
View File
@@ -69,7 +69,7 @@ export const mailRules: DoctorRule[] = [
return [{ fix: 'set the four GRAPH_* variables on the server — or choose another sending method in Site integrations → E-mail sending (Microsoft Graph is deprecated and removed in 2.0)', message: '"Microsoft Graph" is the chosen sending method, but the GRAPH_* variables are missing on this server: e-mails are not sent (nothing is sent another way)', severity: 'error' }]
}
if (mail.methodChosen === false && mail.transport !== 'none') {
return [{ fix: 'open Site integrations → E-mail sending, check "Sending method" and save: from then on the site sends that way only', message: `nobody has chosen the sending method yet: the site picks one from what is filled in (now: ${mail.transport === 'relay' ? 'the built-in service' : mail.transport === 'smtp' ? 'the SMTP server' : 'Microsoft Graph'}), and would switch by itself if that changed`, severity: 'info' }]
return [{ fix: 'nothing to do while it sends the way it should; to fix the method, pick it in Site integrations → E-mail sending (another option, or setMailMethod)', message: `nobody has picked a sending method: the site sends the way it did before the upgrade (now: ${mail.transport === 'relay' ? 'the built-in service' : mail.transport === 'smtp' ? 'the SMTP server' : 'Microsoft Graph'}) and follows what is filled in, as before`, severity: 'info' }]
}
return []
},
+3
View File
@@ -31,6 +31,8 @@ export type MailPanelState = {
accessKeyField?: boolean
/** The address the service calls this site back at. */
confirmUrl?: string
/** The four `GRAPH_*` variables are set on this server (whether "Microsoft Graph" could send). */
graphReady: boolean
/**
* An admin chose the method in effect ("Sending method" was saved). False: nobody has chosen yet, and the
* method follows the order of 1.17.0 until Site integrations is saved.
@@ -100,6 +102,7 @@ export async function mailPanelState(req: Pick<PayloadRequest, 'payload'>, own:
return {
...(options?.accessKeyField === true ? { accessKeyField: true } : {}),
...(status?.host ? { confirmUrl: `https://${status.host}${api}${RELAY_CONFIRM_PATH}` } : {}),
graphReady: graphAvailable(),
methodChosen: method.explicit,
...(mailMethodColumnMissing() ? { methodColumnMissing: true } : {}),
...(method.problem ? { methodProblem: method.problem } : {}),