WIP: sending method as an explicit choice, kept in payload-kv (superseded: the choice moves to a stored field)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
68a2555277
commit
c6841a1674
@@ -6,6 +6,7 @@ import { assertDevelopmentHosts } from '../shared/developmentHosts.js'
|
||||
import { sendAccountMailAfterCommit } from './accountMailAfterCommit.js'
|
||||
import { graphAdapter, type GraphAdapterArgs, graphLargeAttachmentSupport } from './graphAdapter.js'
|
||||
import { attachmentBytes, GRAPH_INLINE_MAX_BYTES, toGraphAttachments } from './graphAttachments.js'
|
||||
import { getMailMethod, type MailMethod } from './method.js'
|
||||
import { panelSmtpAdapter, type PanelSmtpAdapterArgs } from './panelSmtpAdapter.js'
|
||||
import { relayAttachmentLimit, sendRelayWithPanel } from './relay/adapter.js'
|
||||
import { pluginMailOptions, type RelayOptions, resolveRelayOptions } from './relay/options.js'
|
||||
@@ -46,6 +47,32 @@ export function resolveMailTransport(settings: TransportIntegrations, state: { g
|
||||
return state.relay ? 'relay' : 'none'
|
||||
}
|
||||
|
||||
/** Why the chosen method cannot be used. The mail is NOT sent another way. */
|
||||
export type MailMethodProblem = 'graph-not-set-up' | 'smtp-incomplete'
|
||||
|
||||
export type ResolvedMailMethod = {
|
||||
/** An admin chose it ("Sending method" was saved); false = nobody has chosen yet, the order of 1.17.0 decides. */
|
||||
explicit: boolean
|
||||
method: MailTransport
|
||||
/** Set only for an explicit choice that cannot be used; sends fail and say so. */
|
||||
problem?: MailMethodProblem
|
||||
}
|
||||
|
||||
/**
|
||||
* The sending method in effect. With a choice (`getMailMethod`): exactly that method — an SMTP choice with
|
||||
* an incomplete server, or Graph without the `GRAPH_*` variables, is a problem and the mail fails loudly;
|
||||
* nothing is sent another way. Without a choice: `resolveMailTransport`, the order of 1.17.0.
|
||||
* A choice of the built-in service counts as no choice when the project switched the service off
|
||||
* (`mail: { relay: false }` removes the option).
|
||||
*/
|
||||
export function resolveMailMethod(settings: TransportIntegrations, state: { chosen: MailMethod | null; graphEnv: boolean; relay: boolean }): ResolvedMailMethod {
|
||||
const chosen = state.chosen === 'relay' && !state.relay ? null : state.chosen
|
||||
if (!chosen) {return { explicit: false, method: resolveMailTransport(settings, state) }}
|
||||
if (chosen === 'smtp') {return { explicit: true, method: 'smtp', ...(smtpConfigured(settings) ? {} : { problem: 'smtp-incomplete' as const }) }}
|
||||
if (chosen === 'graph') {return { explicit: true, method: 'graph', ...(state.graphEnv ? {} : { problem: 'graph-not-set-up' as const }) }}
|
||||
return { explicit: true, method: 'relay' }
|
||||
}
|
||||
|
||||
/** 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'])
|
||||
|
||||
@@ -137,11 +164,11 @@ export const mailAdapter =
|
||||
* 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 methodOf = async (settings: TransportIntegrations) => resolveMailMethod(settings, { chosen: await getMailMethod(payload), graphEnv: graphAvailable(), relay: Boolean(relayOptions()) })
|
||||
|
||||
const attachmentLimit = async (): Promise<null | number> => {
|
||||
const settings = await getSiteIntegrations<TransportIntegrations>(payload)
|
||||
const transport = transportOf(settings)
|
||||
const transport = (await methodOf(settings)).method
|
||||
// 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}
|
||||
@@ -168,21 +195,30 @@ export const mailAdapter =
|
||||
const settings = await getSiteIntegrations<TransportIntegrations>(payload)
|
||||
const choice = settings.emailTransport ?? 'smtp'
|
||||
const previous = settings[STATUS_FIELD]
|
||||
const chosen = transportOf(settings)
|
||||
const resolved = await methodOf(settings)
|
||||
const chosen = resolved.method
|
||||
|
||||
// No SMTP server in the panel and no Graph: the Intecion Notifications Webservice.
|
||||
if (chosen === 'relay') {
|
||||
// Half an SMTP set-up was "not configured" before 1.17.0; now the mail leaves another way: say so once.
|
||||
if (smtpIncomplete(settings) && !warnedSmtpIncomplete) {
|
||||
if (!resolved.explicit && smtpIncomplete(settings) && !warnedSmtpIncomplete) {
|
||||
warnedSmtpIncomplete = true
|
||||
payload.logger.warn('[iapl] The SMTP settings in Site integrations are incomplete (server, user name and password are all needed), so e-mails go through the Intecion Notifications Webservice. Complete them to use the SMTP server, or empty them.')
|
||||
}
|
||||
return sendRelayWithPanel(payload, relayOptions()!, message, settings)
|
||||
}
|
||||
|
||||
// Graph was chosen, but this server has no GRAPH_* variables: the mail fails, and says so. It does not
|
||||
// leave another way — what is chosen is what is used.
|
||||
if (resolved.problem === 'graph-not-set-up') {
|
||||
const error = 'Microsoft Graph is chosen as the sending method, but it is not set up on this server (the GRAPH_* variables are missing). Nothing was sent.'
|
||||
await recordIntegrationStatus(payload, STATUS_FIELD, previous, { code: 'graph-not-set-up', message: error, state: 'degraded' })
|
||||
return { code: 'graph-not-set-up', error, sent: false }
|
||||
}
|
||||
|
||||
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
|
||||
const graphFallback = !resolved.explicit && choice === 'graph' && !useGraph
|
||||
|
||||
if (graphFallback) {
|
||||
// Panel asked for Graph but the agency creds aren't configured for
|
||||
@@ -206,7 +242,8 @@ export const mailAdapter =
|
||||
const viaGraph = useGraph && !largeOverSmtp
|
||||
|
||||
const transport = viaGraph ? 'Microsoft Graph' : 'SMTP'
|
||||
const code = viaGraph ? 'graph-error' : 'smtp-error'
|
||||
// (SMTP was chosen and its settings are incomplete: its own code, so the panel can say what to fill in)
|
||||
const code = viaGraph ? 'graph-error' : resolved.problem === 'smtp-incomplete' ? 'smtp-incomplete' : 'smtp-error'
|
||||
let result: unknown
|
||||
try {
|
||||
result = await (viaGraph ? graph : smtp).sendEmail(message)
|
||||
|
||||
Reference in New Issue
Block a user