import type { DoctorContext } from '../project.js' import type { DoctorRule, RuleFinding } from '../types.js' import { configFile, nextConfigFile } from '../project.js' import { stripComments } from '../util.js' const CONFIRM = '/iapl/mail-relay/confirm' const cfg = (ctx: DoctorContext) => stripComments(ctx.read(configFile(ctx) ?? '')) const next = (ctx: DoctorContext) => stripComments(ctx.read(nextConfigFile(ctx) ?? '')) const relayOff = (code: string) => /\brelay\s*:\s*false\b/.test(code) const since = (iso?: null | string) => (iso ? `, since ${iso.slice(0, 16).replace('T', ' ')}` : '') /** * E-mail (docs/email.md): which transport is in effect and whether it works, the site's state with the * Intecion Notifications Webservice, the deprecated Graph transport, the reply address, and whether * the service can reach the site's confirm route. Ids carry no brand, like `agents-md`. */ /** "this site is non-public because …" — the signal named, for the findings that depend on it. */ const nonPublicBecause = (mail: { nonPublicBy?: 'development-host' | 'noindex'; nonPublicPattern?: string }): string => mail.nonPublicBy === 'noindex' ? 'this site is non-public because IAPL_NOINDEX is set' : mail.nonPublicBy === 'development-host' ? `this site is non-public because its host matches the development pattern "${mail.nonPublicPattern ?? '?'}" (developmentHosts)` : 'this site is non-public (a development host, or IAPL_NOINDEX)' export const mailRules: DoctorRule[] = [ { // Which transport a send takes right now, and whether the last sends worked. id: 'mail-transport', check: (ctx) => { const mail = ctx.deep?.mail if (!mail) {return []} const out: RuleFinding[] = [] if (mail.transport === 'none') { out.push({ fix: 'remove `mail: { relay: false }` to send through the Intecion mail service, or fill in an SMTP server in Site integrations → E-mail sending', message: 'no e-mail transport is set up: the Intecion mail service is switched off and the panel has no SMTP server — e-mails are not sent', severity: 'error', }) } if (mail.status?.state === 'degraded') { const now = ctx.today === new Date().toISOString().slice(0, 10) ? Date.now() : Date.parse(`${ctx.today}T12:00:00Z`) const hours = mail.status.since ? Math.floor((now - Date.parse(mail.status.since)) / 3_600_000) : null out.push({ fix: 'Site integrations → E-mail sending: read the state block, then send a test e-mail (iapl-debug, "E-mail")', message: `e-mail sending is failing (${mail.status.code ?? 'unknown'}, transport: ${mail.transport})${since(mail.status.since)}`, severity: hours !== null && hours >= 24 ? 'error' : 'warning', }) } return out }, docs: 'docs/email.md', group: 'mail', source: 'deep', }, { // "Sending method": what is chosen is what is used. A choice that cannot be used sends nothing. id: 'mail-method', check: (ctx) => { const mail = ctx.deep?.mail if (!mail) {return []} if (mail.methodColumnMissing) { return [{ fix: 'create and apply the migration: `pnpm payload migrate:create`, commit it, and have it applied when the new container starts (docs/email.md "Which transport a send takes" → Postgres and SQLite)', message: 'the database has no column for "Sending method" (`emailMethod`, new in 1.17.1): e-mails still go the way they did before the upgrade, but the Site integrations page of the panel does not open and the method cannot be chosen', severity: 'error' }] } if (mail.methodProblem === 'smtp-incomplete') { return [{ fix: 'Site integrations → E-mail sending: fill in server, user name and password — or choose another sending method', message: '"Own SMTP server" is the chosen sending method, but its settings are incomplete: e-mails are not sent (nothing is sent another way)', severity: 'error' }] } if (mail.methodProblem === 'graph-not-set-up') { 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: '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 [] }, docs: 'docs/email.md', group: 'mail', source: 'deep', }, { // Which signal makes the site a non-public copy for the mail service — said plainly, because that signal // decides whether a copy of another site's data mails real people. id: 'mail-non-public', check: (ctx) => { const mail = ctx.deep?.mail if (!mail || mail.transport !== 'relay' || !mail.relay || !mail.nonPublic) {return []} return [{ fix: mail.nonPublicBy === 'noindex' ? 'right for a development, staging or test copy. On the PUBLIC site this is a mistake: remove IAPL_NOINDEX there (it also keeps the site out of search engines)' : 'right for a development address. If this host is the PUBLIC site, take it out of the list: iapl({ developmentHosts: [...] }) (`[]` = no development hosts)', message: `${nonPublicBecause(mail)}: for the Intecion mail service it is a non-public site — it sends its own e-mails, but with a copy of another site's data it sends nothing by itself`, severity: 'info', }] }, docs: 'docs/email.md', group: 'mail', source: 'deep', }, { // The site's state with the Intecion mail service, when that is how it sends. id: 'mail-relay-state', check: (ctx) => { const mail = ctx.deep?.mail const relay = mail?.relay if (!mail || mail.transport !== 'relay' || !relay) {return []} const out: RuleFinding[] = [] if (relay.disconnected) { out.push({ fix: 'Site integrations → E-mail sending → "Connect again", or fill in an SMTP server', message: 'the site was disconnected from the Intecion mail service in the panel: e-mails are not sent', severity: 'error' }) } else if (relay.state === 'suspended') { out.push({ fix: 'contact Intecion; meanwhile an SMTP server in the panel sends the site\'s mail', message: 'Intecion has stopped e-mail sending for this site (suspended)', severity: 'error' }) } else if (relay.state === 'pending') { out.push({ fix: 'Intecion approves the host on the service (`ims approve `); the same token then works', message: 'the site is enrolled with the Intecion mail service but waits for approval: e-mails are not sent yet', severity: 'warning' }) } else if (relay.refusal === 'client-other-host') { out.push({ fix: 'expected for a copy that must not mail real people. If this copy should send e-mails: Site integrations → E-mail sending → "Connect again" (once). To keep it silent for good: mail: { relay: false } or SMTP to a catcher. If this is NOT a copy but the site itself, it is wrongly marked non-public: remove IAPL_NOINDEX, or take the host out of iapl({ developmentHosts })', message: `${nonPublicBecause(mail)} and its database holds the mail-service connection of another site (${relay.reason ?? 'another host'}) — a copy of that site's data: it does not connect or send by itself, e-mails are not sent`, severity: 'warning', }) } else if (!relay.connected && relay.refusal) { const path = relay.refusal === 'callback-failed' ? ` (${relay.reason ?? 'unknown'}): the service could not confirm the site at its public address` : '' out.push({ fix: relay.refusal === 'callback-failed' ? 'the site must answer POST /api/iapl/mail-relay/confirm from the internet without a redirect (email.md, "When the site cannot be confirmed"), then "Connect again"' : 'email.md, "When the site cannot be confirmed"; then Site integrations → E-mail sending → "Connect again"', message: `the Intecion mail service did not connect this site: ${relay.refusal}${path}`, severity: 'warning', }) } else if (!relay.connected) { out.push({ fix: 'send a test e-mail from Site integrations → E-mail sending', message: 'the site has not enrolled with the Intecion mail service yet (it does so on its first e-mail)', severity: 'info' }) } if (mail.otherHostEntries && !mail.nonPublic) { // Right after a launch or a domain move this is the site's own past. On a COPY that forgot to say it is not // public it is the last warning before real people get its mail. out.push({ fix: 'the site itself after a launch or a move: nothing to do — name the old host to silence this: mail: { relay: { otherHosts: [\'\'] } }. A staging or test COPY of that site: set IAPL_NOINDEX=1 there (it then waits for "Connect again"), or mail: { relay: false } / SMTP to a catcher', message: 'the database also holds the mail-service connection of another host, and this site is public: if it is a copy of that site, it sends e-mails to the real addresses in its data', severity: 'info', }) } if (relay.nameReplaced) { out.push({ fix: 'Site integrations → E-mail sending → Sender name: a name that is the client\'s own (no address, no other company\'s name)', message: 'the mail service did not accept the sender name, so e-mails leave under the site\'s host', severity: 'warning' }) } return out }, docs: 'docs/email.md', group: 'mail', source: 'deep', }, { // The sender address of the service has no mailbox: without a reply address, answers are lost. id: 'mail-reply-address', check: (ctx) => { const mail = ctx.deep?.mail return mail?.transport === 'relay' && !mail.replyAddress ? [{ fix: 'Site integrations → E-mail sending → Sender address: the client\'s own address', message: 'no reply address is set: replies to the site\'s e-mails (other than forms that set their own) go to an address without a mailbox', severity: 'warning' }] : [] }, docs: 'docs/email.md', group: 'mail', source: 'deep', }, { // Microsoft Graph as the transport: deprecated in 1.17.0, removed in 2.0. id: 'mail-graph-deprecated', check: (ctx) => { const mail = ctx.deep?.mail const inCode = /\bgraphAdapter\s*\(|\bemailGraph\s*:\s*true\b/.test(cfg(ctx)) if (!mail?.graphEnv && mail?.transport !== 'graph' && !inCode) {return []} return [{ fix: 'remove the four GRAPH_* variables (Coolify) and send a test e-mail — the site then uses the Intecion mail service — or fill in an SMTP server (email.md, "Moving off Microsoft Graph")', message: mail?.transport === 'graph' ? 'e-mails go through Microsoft Graph, which is deprecated and will be removed in 2.0' : 'Microsoft Graph is still set up for this site (GRAPH_* variables or graphAdapter); it is deprecated and will be removed in 2.0', severity: 'warning', }] }, docs: 'docs/email.md', group: 'mail', source: 'static', }, { // The service calls exactly https:///api/iapl/mail-relay/confirm and follows no redirect. id: 'mail-relay-route', check: async (ctx) => { const code = cfg(ctx) if (relayOff(code)) {return []} const out: RuleFinding[] = [] const ask = 'ask Intecion to set this site\'s confirm address (`ims callback-path `), or change the site to answer at /api/iapl/mail-relay/confirm' if (/\btrailingSlash\s*:\s*true\b/.test(next(ctx))) { out.push({ file: nextConfigFile(ctx) ?? 'next.config.ts', fix: `${ask} — with trailing slashes the path ends in /confirm/`, message: 'trailingSlash: true redirects the mail service\'s call-back, and the service follows no redirect: the site cannot enrol', severity: 'warning' }) } const api = /\broutes\s*:\s*\{[^}]*\bapi\s*:\s*['"`]([^'"`]+)['"`]/.exec(code)?.[1] if (api && api !== '/api') { out.push({ file: configFile(ctx) ?? 'src/payload.config.ts', fix: ask, message: `the API is served at ${api}, not /api: the mail service calls /api${CONFIRM} unless Intecion set another address for this host`, severity: 'warning' }) } if (ctx.live && ctx.deep?.mail?.transport === 'relay') { const res = await ctx.live.fetch(`${api ?? '/api'}${CONFIRM}`, { redirect: 'manual' }) // A GET finds no route there (the route is POST): 404 or 405 means the address is reached as it is. if (res.status >= 300 && res.status < 400) { out.push({ fix: `enrol the host the site really answers on (mail.relay.host), or ${ask}`, message: `live ${api ?? '/api'}${CONFIRM} redirects (${res.status} → ${res.headers.get('location') ?? '?'}); the mail service follows no redirect`, severity: 'warning' }) } else if (res.status === 401 || res.status === 403 || res.status === 0 || res.status >= 500) { out.push({ fix: 'the confirm route must be reachable from the internet: no basic auth, no firewall rule, no maintenance page in front of it', message: `live ${api ?? '/api'}${CONFIRM} → ${res.status || res.error}: the mail service cannot call the site back`, severity: 'warning' }) } } return out }, docs: 'docs/email.md', group: 'mail', source: 'static', }, ]