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
@@ -3,7 +3,8 @@ import type { Endpoint, PayloadRequest } from 'payload'
|
||||
import { addDataAndFileToRequest } from 'payload'
|
||||
|
||||
import { userMeetsRole } from '../../access/predicates.js'
|
||||
import { serverText } from '../../integrations/texts.js'
|
||||
import { serverText, STATUS_CODE_TEXTS } from '../../integrations/texts.js'
|
||||
import { withMailKind } from '../relay/kinds.js'
|
||||
|
||||
/**
|
||||
* Custom endpoint: send a test email to a given address through whatever
|
||||
@@ -64,7 +65,7 @@ export const testEmailEndpoint: Endpoint = {
|
||||
const text = site ? serverText(i18n, 'testEmailBody', { site }) : serverText(i18n, 'testEmailBodyGeneric')
|
||||
|
||||
try {
|
||||
const info = await req.payload.sendEmail({
|
||||
const info = await req.payload.sendEmail(withMailKind({
|
||||
...(attachmentMB
|
||||
? { attachments: [{ content: Buffer.alloc(attachmentMB * 1024 * 1024, 'IAPL test file\n'), contentType: 'text/plain', filename: `test-${attachmentMB}MB.txt` }] }
|
||||
: {}),
|
||||
@@ -72,7 +73,7 @@ export const testEmailEndpoint: Endpoint = {
|
||||
subject,
|
||||
text,
|
||||
to,
|
||||
})
|
||||
}, 'test'))
|
||||
|
||||
// Payload's sendEmail resolves with the adapter's result. Our adapters
|
||||
// return { sent: boolean, error?: string, code?: string }; nodemailer
|
||||
@@ -83,16 +84,28 @@ export const testEmailEndpoint: Endpoint = {
|
||||
: true
|
||||
|
||||
if (!sent) {
|
||||
const failed = info as { code?: string; detail?: string; error?: string }
|
||||
const failed = info as { code?: string; confirmed?: boolean; detail?: string; error?: string }
|
||||
req.payload.logger.error(`[iapl] Test email not sent: ${failed?.detail ?? failed?.error ?? 'unknown reason'}`)
|
||||
const relayText = failed?.code ? STATUS_CODE_TEXTS[failed.code] : undefined
|
||||
const error =
|
||||
failed?.code === 'not-configured'
|
||||
? serverText(i18n, 'testEmailNotConfigured')
|
||||
: serverText(i18n, 'testEmailFailed')
|
||||
return Response.json({ error, ok: false }, { status: 502 })
|
||||
: failed?.confirmed === false
|
||||
? serverText(i18n, 'testEmailNotConfirmed')
|
||||
// the mail service's refusal in the admin's language (waiting for approval, a limit, too large …)
|
||||
: failed?.code?.startsWith('relay-') && relayText
|
||||
? serverText(i18n, relayText)
|
||||
: serverText(i18n, 'testEmailFailed')
|
||||
return Response.json({ code: failed?.code, error, ok: false }, { status: 502 })
|
||||
}
|
||||
|
||||
return Response.json({ message: serverText(i18n, 'testEmailSent', { to }), ok: true })
|
||||
// The answer names the transport that carried it.
|
||||
const sentBy = info as { senderName?: string; transport?: string } | null
|
||||
const message =
|
||||
sentBy?.transport === 'relay' ? serverText(i18n, 'testEmailSentRelay', { name: sentBy.senderName ?? '', to })
|
||||
: sentBy?.transport === 'smtp' ? serverText(i18n, 'testEmailSentSmtp', { to })
|
||||
: serverText(i18n, 'testEmailSent', { to })
|
||||
return Response.json({ message, ok: true, ...(sentBy?.transport ? { transport: sentBy.transport } : {}) })
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
req.payload.logger.error(`[iapl] Test email failed: ${message}`)
|
||||
|
||||
Reference in New Issue
Block a user