Files
petvity/lib/email/index.ts
T
5236175232 refactor(email): migrate transport to @bitbaum/mail-kit (#78)
Replace the bespoke Resend-SDK transport in lib/email/index.ts with the
fleet's shared @bitbaum/mail-kit. Contract preserved: {sent:false} when
mail is unconfigured or the recipient is an undeliverable fixture, throw
on provider API errors so the cron can mark queue rows failed. The
unconfigured gate is now mail-kit's isMailConfigured, which also rejects
placeholder keys and sandbox senders in production. Module-load FROM
replaced by call-time RESEND_FROM with the fleet-conventional fallback.
Transport tests now mock global fetch. Removes the resend dependency.


Claude-Session: https://claude.ai/code/session_01WqKqMnHQHSmkGFfc5t7Rxn

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-05 10:45:21 +00:00

48 lines
1.6 KiB
TypeScript

import { sendMail, isMailConfigured, fromAddress, conventionalFrom } from "@bitbaum/mail-kit";
import { APP } from "@/lib/config/app";
import { isUndeliverableRecipient } from "@/lib/config/email";
export type SendEmailOptions = {
to: string;
subject: string;
html: string;
};
/**
* Send a transactional email via @bitbaum/mail-kit (the fleet's one email
* layer). Returns { sent: true } on success, { sent: false } when mail is not
* configured (dev/staging, placeholder key, sandbox sender in production) or
* the recipient cannot receive mail — never throws for either.
* Throws for provider API errors so callers can decide to retry or log.
*/
export async function sendEmail({
to,
subject,
html,
}: SendEmailOptions): Promise<{ sent: boolean }> {
// Test fixtures are real users in a real database, so they get real mail —
// at domains with no mailbox. Every such send is a hard bounce charged
// against the reputation that carries real owners' password resets.
// See lib/config/email.ts.
if (isUndeliverableRecipient(to)) {
process.stderr.write(`[email] undeliverable recipient — not sending "${subject}"\n`);
return { sent: false };
}
if (!isMailConfigured()) {
if (process.env.NODE_ENV !== "production") {
process.stderr.write(`[email] mail not configured — skipping "${subject}"\n`);
}
return { sent: false };
}
const result = await sendMail({
to,
subject,
html,
from: fromAddress() ?? conventionalFrom(APP.name),
});
if (!result.sent) throw new Error(`Resend error: ${result.error}`);
return { sent: true };
}