Mail relay: the built-in client for the Intecion Notifications Webservice
Transport, enrolment with the confirm route, token and state in payload-kv, rotation, the panel's state block, doctor rules, the fake service and the copied contract 1.5.0. Tests against the contract's runner and the fake. Docs and HTTP tests follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
9efd86a06f
commit
ea9703f8e3
@@ -5,11 +5,15 @@ import { getSiteIntegrations } from '../payload/index.js'
|
||||
import { graphAdapter, type GraphAdapterArgs, graphLargeAttachmentSupport } from './graphAdapter.js'
|
||||
import { attachmentBytes, GRAPH_INLINE_MAX_BYTES, toGraphAttachments } from './graphAttachments.js'
|
||||
import { panelSmtpAdapter, type PanelSmtpAdapterArgs } from './panelSmtpAdapter.js'
|
||||
import { relayAttachmentLimit, sendRelayWithPanel } from './relay/adapter.js'
|
||||
import { pluginMailOptions, type RelayOptions, resolveRelayOptions } from './relay/options.js'
|
||||
|
||||
type TransportIntegrations = {
|
||||
emailStatus?: IntegrationStatus | null
|
||||
/** 'smtp' | 'graph' — chosen by the editor in SiteIntegrations. */
|
||||
emailTransport?: 'graph' | 'smtp' | null
|
||||
smtpFromAddress?: null | string
|
||||
smtpFromName?: null | string
|
||||
smtpHost?: null | string
|
||||
smtpPassword?: null | string
|
||||
smtpUser?: null | string
|
||||
@@ -17,9 +21,33 @@ type TransportIntegrations = {
|
||||
|
||||
const smtpConfigured = (s: TransportIntegrations) => Boolean(s.smtpHost && s.smtpUser && s.smtpPassword)
|
||||
|
||||
/** Which transport a send takes. `none`: nothing is set up and the relay is switched off. */
|
||||
export type MailTransport = 'graph' | 'none' | 'relay' | 'smtp'
|
||||
|
||||
/**
|
||||
* The transport in effect for the present settings, decided on every send (no stored value):
|
||||
* 1. "Sending method" is Microsoft Graph and the GRAPH_* variables are complete → Graph, as before
|
||||
* (deprecated; removed in 2.0);
|
||||
* 2. the panel has an SMTP server (server, user name, password) → SMTP — on the client's request;
|
||||
* 3. the GRAPH_* variables are complete → Graph (deprecated), until they are removed;
|
||||
* 4. otherwise → the Intecion Notifications Webservice, unless it is switched off (`mail: { relay: false }`);
|
||||
* 5. otherwise nothing is configured.
|
||||
* A site with SMTP filled in never contacts the service.
|
||||
*/
|
||||
export function resolveMailTransport(settings: TransportIntegrations, state: { graphEnv: boolean; relay: boolean }): MailTransport {
|
||||
if ((settings.emailTransport ?? 'smtp') === 'graph' && state.graphEnv) {return 'graph'}
|
||||
if (smtpConfigured(settings)) {return 'smtp'}
|
||||
if (state.graphEnv) {return 'graph'}
|
||||
return state.relay ? 'relay' : 'none'
|
||||
}
|
||||
|
||||
/** Graph result codes that mean "Graph cannot carry an attachment this large" — SMTP may. */
|
||||
const GRAPH_TOO_LARGE = new Set(['graph-large-off', 'graph-permission'])
|
||||
|
||||
/** The one warning a site on Microsoft Graph gets per process (silence: `mail: { graphDeprecationWarning: false }`). */
|
||||
export const GRAPH_DEPRECATION_NOTICE =
|
||||
'[iapl] Microsoft Graph as the e-mail transport is deprecated and will be removed in 2.0. Remove the GRAPH_* variables to send through the Intecion Notifications Webservice, or fill in an SMTP server in Site integrations (docs/email.md, "Moving off Microsoft Graph").'
|
||||
|
||||
const STATUS_FIELD = 'emailStatus'
|
||||
/** Error text for the panel: first line, no stack, capped — SMTP errors don't carry the password. */
|
||||
const describe = (err: unknown) =>
|
||||
@@ -28,12 +56,27 @@ const describe = (err: unknown) =>
|
||||
export type MailAdapterArgs = {
|
||||
fallbackFromAddress?: string
|
||||
fallbackFromName?: string
|
||||
/** @deprecated Microsoft Graph is deprecated since 1.17.0 and removed in 2.0 (docs/email.md, "Moving off Microsoft Graph"). */
|
||||
graph?: GraphAdapterArgs
|
||||
/**
|
||||
* The Intecion Notifications Webservice — the transport of a site with no SMTP server in the panel.
|
||||
* `false` switches it off for this adapter; an object overrides `iapl({ mail: { relay } })`.
|
||||
*/
|
||||
relay?: false | RelayOptions
|
||||
smtp?: PanelSmtpAdapterArgs
|
||||
}
|
||||
|
||||
let warnedGraphDeprecated = false
|
||||
/** iapl() left its options in the config: it has already warned at start (plugin.ts, onInit). */
|
||||
const pluginWarnsAtStart = (payload: { config?: { custom?: unknown } }) => Boolean((payload.config?.custom as { iapl?: { mail?: unknown } } | undefined)?.iapl?.mail)
|
||||
|
||||
/** For tests. */
|
||||
export function resetGraphDeprecationWarning(): void {
|
||||
warnedGraphDeprecated = false
|
||||
}
|
||||
|
||||
/** True when the agency Graph credentials are present in the environment. */
|
||||
function graphAvailable(): boolean {
|
||||
export function graphAvailable(): boolean {
|
||||
return Boolean(
|
||||
process.env.GRAPH_TENANT_ID &&
|
||||
process.env.GRAPH_CLIENT_ID &&
|
||||
@@ -44,7 +87,9 @@ function graphAvailable(): boolean {
|
||||
|
||||
/**
|
||||
* Dispatcher email adapter: wired into the config ONCE, but picks the transport
|
||||
* (SMTP or Graph) per send by reading `emailTransport` from SiteIntegrations.
|
||||
* per send from Site integrations (resolveMailTransport): the panel's SMTP server
|
||||
* when one is filled in, Microsoft Graph while its variables last (deprecated),
|
||||
* otherwise the Intecion Notifications Webservice (relay/).
|
||||
* This is what makes the choice switchable in the panel — Payload builds the
|
||||
* email adapter at boot and can't swap it at runtime, so instead of choosing
|
||||
* between two adapters at boot we install one that delegates on every send.
|
||||
@@ -84,9 +129,15 @@ export const mailAdapter =
|
||||
* promises more than can be sent: on Graph without the large-file path and
|
||||
* without SMTP settings the ceiling is GRAPH_INLINE_MAX_BYTES.
|
||||
*/
|
||||
const relayOptions = () => resolveRelayOptions(payload, args.relay)
|
||||
const transportOf = (settings: TransportIntegrations) => resolveMailTransport(settings, { graphEnv: graphAvailable(), relay: Boolean(relayOptions()) })
|
||||
|
||||
const attachmentLimit = async (): Promise<null | number> => {
|
||||
const settings = await getSiteIntegrations<TransportIntegrations>(payload)
|
||||
if ((settings.emailTransport ?? 'smtp') !== 'graph' || !graphAvailable()) {return null}
|
||||
const transport = transportOf(settings)
|
||||
// The service says what it carries for this site (10 MB unless Intecion set another number).
|
||||
if (transport === 'relay') {return relayAttachmentLimit(payload, relayOptions()!).catch(() => null)}
|
||||
if (transport !== 'graph') {return null}
|
||||
const support = await graphLargeAttachmentSupport(args.graph)
|
||||
if (support === 'ok' || support === 'unavailable' || smtpConfigured(settings)) {return null}
|
||||
return GRAPH_INLINE_MAX_BYTES - 1
|
||||
@@ -102,7 +153,13 @@ export const mailAdapter =
|
||||
const settings = await getSiteIntegrations<TransportIntegrations>(payload)
|
||||
const choice = settings.emailTransport ?? 'smtp'
|
||||
const previous = settings[STATUS_FIELD]
|
||||
const useGraph = choice === 'graph' && graphAvailable()
|
||||
const chosen = transportOf(settings)
|
||||
|
||||
// No SMTP server in the panel and no Graph: the Intecion Notifications Webservice.
|
||||
if (chosen === 'relay') {return sendRelayWithPanel(payload, relayOptions()!, message, settings)}
|
||||
|
||||
const useGraph = chosen === 'graph'
|
||||
// "Graph" in the panel without its variables is reported only while SMTP carries the mail instead.
|
||||
const graphFallback = choice === 'graph' && !useGraph
|
||||
|
||||
if (graphFallback) {
|
||||
@@ -112,6 +169,11 @@ export const mailAdapter =
|
||||
'[iapl] Transport set to Graph but GRAPH_* env vars are missing; falling back to SMTP.',
|
||||
)
|
||||
}
|
||||
// A project that uses mailAdapter() without iapl()'s start warning still hears it once.
|
||||
if (useGraph && !warnedGraphDeprecated && !pluginWarnsAtStart(payload) && pluginMailOptions(payload).graphDeprecationWarning !== false) {
|
||||
warnedGraphDeprecated = true
|
||||
payload.logger.warn(GRAPH_DEPRECATION_NOTICE)
|
||||
}
|
||||
|
||||
// A large attachment Graph cannot carry (switched off, or the app lacks
|
||||
// Mail.ReadWrite) goes over SMTP when the panel has SMTP settings.
|
||||
@@ -168,7 +230,8 @@ export const mailAdapter =
|
||||
}
|
||||
: { state: 'ok' },
|
||||
)
|
||||
return result
|
||||
// which transport carried it — the test e-mail says so in the panel
|
||||
return result && typeof result === 'object' ? { ...result, transport: viaGraph ? 'graph' : 'smtp' } : result
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user