email-failover-postmark-mailersend.ts
// IOSOR 2027 Multi-Rail Transactional Email Router: Postmark (Primary) -> MailerSend (PAYG Standby)
// Official Docs: https://postmarkapp.com/developer | Standby Docs: https://developers.mailersend.com/
export interface EmailDispatchPayload {
from: string;
to: string;
subject: string;
html: string;
idempotencyKey: string; // Prevents duplicate delivery on retry
}
export async function dispatchEmailWithFailover(payload: EmailDispatchPayload) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 2500); // p95 SMTP/API SLA guard (2.5s)
try {
const res = await fetch(process.env.POSTMARK_EMAIL_API_URL!, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.POSTMARK_API_TOKEN}`,
'Content-Type': 'application/json',
'X-Idempotency-Key': payload.idempotencyKey,
},
body: JSON.stringify(payload),
signal: controller.signal,
});
clearTimeout(timeout);
if (res.ok) return { provider: 'postmark', status: 'sent', code: res.status };
throw new Error(`Postmark HTTP ${res.status}`);
} catch {
clearTimeout(timeout);
// Automatic zero-idle failover to MailerSend
const fb = await fetch(process.env.MAILERSEND_EMAIL_API_URL!, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.MAILERSEND_API_TOKEN}`,
'Content-Type': 'application/json',
'X-Idempotency-Key': payload.idempotencyKey,
},
body: JSON.stringify(payload),
});
return { provider: 'mailersend', status: 'failover_sent', code: fb.status };
}
}