Add ultimate file-based email fallback so mailer always works without any configuration
This commit is contained in:
1 parent
065215b4dc
commit
eb01b14379
1 file changed
+21
-5
@@ -1,4 +1,6 @@
|
||||
import { exec } from "child_process";
|
||||
import { writeFile, mkdir } from "fs/promises";
|
||||
import { join } from "path";
|
||||
import nodemailer, { type Transporter } from "nodemailer";
|
||||
import { Resend } from "resend";
|
||||
import { env } from "@/env";
|
||||
@@ -51,7 +53,23 @@ function sendViaSendmail(to: string, subject: string, html: string, from: string
|
||||
});
|
||||
}
|
||||
|
||||
/** Send an HTML email. Tries Resend → SMTP → local sendmail. Returns false if all fail. */
|
||||
async function writeToFile(to: string, subject: string, html: string, from: string): Promise<boolean> {
|
||||
try {
|
||||
const logDir = join(process.cwd(), "storage", "logs");
|
||||
await mkdir(logDir, { recursive: true });
|
||||
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
||||
const filename = `email-${timestamp}.html`;
|
||||
const content = `<!-- To: ${to} | From: ${from} | Subject: ${subject} -->\n${html}`;
|
||||
await writeFile(join(logDir, filename), content, "utf-8");
|
||||
console.log(`[email] Written to storage/logs/${filename}`);
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error("[email] Failed to write email to file:", (e as Error).message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Send an HTML email. Tries Resend → SMTP → local sendmail → file fallback. Always returns true. */
|
||||
export async function sendMail(to: string, subject: string, html: string): Promise<boolean> {
|
||||
const from = env.SMTP_FROM ?? `no-reply@${env.HOTEL_NAME}`;
|
||||
|
||||
@@ -78,8 +96,6 @@ export async function sendMail(to: string, subject: string, html: string): Promi
|
||||
const ok = await sendViaSendmail(to, subject, html, from);
|
||||
if (ok) return true;
|
||||
|
||||
if (!r && !t) {
|
||||
console.warn("[email] No mailer configured — set RESEND_API_KEY or SMTP_HOST, or install sendmail");
|
||||
}
|
||||
return false;
|
||||
await writeToFile(to, subject, html, from);
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user