diff --git a/lib/email/mail-service.ts b/lib/email/mail-service.ts index f1f30b6..b37ea8f 100644 --- a/lib/email/mail-service.ts +++ b/lib/email/mail-service.ts @@ -24,13 +24,16 @@ const RETRY_DELAY_MS = 1500; function sleep(ms: number): Promise { return new Promise((r) => setTimeout(r, ms)); } - -/** Basic email format validation (RFC 5322 simplified). */ function isValidEmail(email: string): boolean { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); } -function log(level: "info" | "warn" | "error", message: string, meta?: Record) { +/** Basic email format validation (RFC 5322 simplified). */ +function log( + level: "info" | "warn" | "error", + message: string, + meta?: Record +) { const entry = { ts: new Date().toISOString(), service: "email", @@ -38,11 +41,10 @@ function log(level: "info" | "warn" | "error", message: string, meta?: Record= 400 && res.status < 500 && res.status !== 429) { const errBody = await res.text(); - log("error", "Brevo rejected email (non-retryable)", { status: res.status, body: errBody, to }); + log("error", "Brevo rejected email (non-retryable)", { + status: res.status, + body: errBody, + to + }); return { ok: false, error: `Brevo error ${res.status}` }; } lastError = `HTTP ${res.status}`; - log("warn", `Email attempt ${attempt}/${MAX_RETRIES} failed`, { status: res.status, to }); + log("warn", `Email attempt ${attempt}/${MAX_RETRIES} failed`, { + status: res.status, + to + }); } catch (err) { lastError = err instanceof Error ? err.message : String(err); - log("warn", `Email attempt ${attempt}/${MAX_RETRIES} threw`, { error: lastError, to }); + log("warn", `Email attempt ${attempt}/${MAX_RETRIES} threw`, { + error: lastError, + to + }); } if (attempt < MAX_RETRIES) { @@ -136,6 +148,10 @@ export async function sendEmail(to: string, subject: string, html: string): Prom } } - log("error", "Email send failed after retries", { to, subject, error: lastError }); + log("error", "Email send failed after retries", { + to, + subject, + error: lastError + }); return { ok: false, error: `Failed after ${MAX_RETRIES} retries: ${lastError}` }; }