Fix turnstile
This commit is contained in:
Vendored
+3
-3
@@ -1,11 +1,11 @@
|
||||
import type { PayloadEmailAdapter } from 'payload';
|
||||
import { type GraphAdapterArgs } from './graphAdapter.js';
|
||||
import { type PanelSmtpAdapterArgs } from './panelSmtpAdapter.js';
|
||||
import { type GraphAdapterArgs } from './graphAdapter.js';
|
||||
export type MailAdapterArgs = {
|
||||
smtp?: PanelSmtpAdapterArgs;
|
||||
graph?: GraphAdapterArgs;
|
||||
fallbackFromAddress?: string;
|
||||
fallbackFromName?: string;
|
||||
graph?: GraphAdapterArgs;
|
||||
smtp?: PanelSmtpAdapterArgs;
|
||||
};
|
||||
/**
|
||||
* Dispatcher email adapter: wired into the config ONCE, but picks the transport
|
||||
|
||||
Vendored
+42
-7
@@ -1,6 +1,9 @@
|
||||
import { getSiteIntegrations } from '../payload/index.js';
|
||||
import { graphAdapter } from './graphAdapter.js';
|
||||
import { recordIntegrationStatus } from '../integrations/status.js';
|
||||
import { panelSmtpAdapter } from './panelSmtpAdapter.js';
|
||||
import { graphAdapter } from './graphAdapter.js';
|
||||
const STATUS_FIELD = 'emailStatus';
|
||||
/** Error text for the panel: first line, no stack, capped — SMTP errors don't carry the password. */ const describe = (err)=>(err instanceof Error ? err.message : String(err)).split('\n')[0].slice(0, 200);
|
||||
/** True when the agency Graph credentials are present in the environment. */ function graphAvailable() {
|
||||
return Boolean(process.env.GRAPH_TENANT_ID && process.env.GRAPH_CLIENT_ID && process.env.GRAPH_CLIENT_SECRET && process.env.GRAPH_SENDER);
|
||||
}
|
||||
@@ -41,16 +44,48 @@ import { panelSmtpAdapter } from './panelSmtpAdapter.js';
|
||||
sendEmail: async (message)=>{
|
||||
const settings = await getSiteIntegrations(payload);
|
||||
const choice = settings.emailTransport ?? 'smtp';
|
||||
if (choice === 'graph') {
|
||||
if (graphAvailable()) {
|
||||
return graph.sendEmail(message);
|
||||
}
|
||||
const previous = settings[STATUS_FIELD];
|
||||
const useGraph = choice === 'graph' && graphAvailable();
|
||||
const graphFallback = choice === 'graph' && !useGraph;
|
||||
if (graphFallback) {
|
||||
// Panel asked for Graph but the agency creds aren't configured for
|
||||
// this project. Fall back to SMTP rather than silently dropping mail.
|
||||
payload.logger.warn('[ipal] Transport set to Graph but GRAPH_* env vars are missing; falling back to SMTP.');
|
||||
return smtp.sendEmail(message);
|
||||
}
|
||||
return smtp.sendEmail(message);
|
||||
const transport = useGraph ? 'Microsoft Graph' : 'SMTP';
|
||||
const code = useGraph ? 'graph-error' : 'smtp-error';
|
||||
let result;
|
||||
try {
|
||||
result = await (useGraph ? graph : smtp).sendEmail(message);
|
||||
} catch (err) {
|
||||
await recordIntegrationStatus(payload, STATUS_FIELD, previous, {
|
||||
state: 'degraded',
|
||||
code,
|
||||
message: `${transport}: ${describe(err)}`
|
||||
});
|
||||
throw err // callers (form-builder, Send test) still see the failure
|
||||
;
|
||||
}
|
||||
// The delegates report failure as { sent: false, error, detail? } instead
|
||||
// of throwing (a broken mailbox must not crash a form submission).
|
||||
const failed = result;
|
||||
if (failed && typeof failed === 'object' && failed.sent === false) {
|
||||
await recordIntegrationStatus(payload, STATUS_FIELD, previous, {
|
||||
state: 'degraded',
|
||||
code,
|
||||
message: `${transport}: ${describe(failed.detail ?? failed.error ?? 'not sent')}`
|
||||
});
|
||||
return result;
|
||||
}
|
||||
// Graph selected but unavailable is a configuration problem even if SMTP delivered.
|
||||
await recordIntegrationStatus(payload, STATUS_FIELD, previous, graphFallback ? {
|
||||
state: 'degraded',
|
||||
code: 'graph-unavailable',
|
||||
message: 'Microsoft Graph is selected but not configured on the server — e-mails are sent over SMTP.'
|
||||
} : {
|
||||
state: 'ok'
|
||||
});
|
||||
return result;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
+12
-10
@@ -32,8 +32,8 @@ import { getSiteIntegrations } from '../payload/index.js';
|
||||
if (!host || !user || !pass) {
|
||||
payload.logger.error('[ipal] Email not sent: SMTP is not configured in Site Integrations.');
|
||||
return {
|
||||
error: 'SMTP is not configured in Site Integrations.',
|
||||
sent: false
|
||||
sent: false,
|
||||
error: 'SMTP is not configured in Site Integrations.'
|
||||
};
|
||||
}
|
||||
// The panel is the source of truth for the sender; fall back to whatever
|
||||
@@ -48,13 +48,13 @@ import { getSiteIntegrations } from '../payload/index.js';
|
||||
const stripCRLF = (v)=>String(v ?? '').replace(/[\r\n]+/g, ' ').trim();
|
||||
const { default: nodemailer } = await import('nodemailer');
|
||||
const transporter = nodemailer.createTransport({
|
||||
auth: {
|
||||
pass,
|
||||
user
|
||||
},
|
||||
host,
|
||||
port,
|
||||
secure: port === 465
|
||||
secure: port === 465,
|
||||
auth: {
|
||||
user,
|
||||
pass
|
||||
}
|
||||
});
|
||||
try {
|
||||
const info = await transporter.sendMail({
|
||||
@@ -65,16 +65,18 @@ import { getSiteIntegrations } from '../payload/index.js';
|
||||
} : {}
|
||||
});
|
||||
return {
|
||||
messageId: info.messageId,
|
||||
sent: true
|
||||
sent: true,
|
||||
messageId: info.messageId
|
||||
};
|
||||
} catch (err) {
|
||||
// Never surface SMTP internals to the caller — a form submission
|
||||
// shouldn't fail loudly because the mailbox is misconfigured.
|
||||
payload.logger.error(`[ipal] Email send failed: ${err.message}`);
|
||||
// `detail` is for the panel status (mailAdapter), not for end users.
|
||||
return {
|
||||
sent: false,
|
||||
error: 'Failed to send email.',
|
||||
sent: false
|
||||
detail: err.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user