/** * SMTP 邮件 — 飞书邮箱(notify@virtheart.com)。 * 凭据全部来自环境变量(.env.local,不入库不入 git)。 */ import nodemailer from "nodemailer"; const SMTP_HOST = process.env.SMTP_HOST || "smtp.feishu.cn"; const SMTP_PORT = Number(process.env.SMTP_PORT || 465); const SMTP_USER = process.env.SMTP_USER || ""; const SMTP_PASS = process.env.SMTP_PASS || ""; const NOTIFY_TO = process.env.NOTIFY_TO || "chenchun@virtheart.com"; let transporter: nodemailer.Transporter | null = null; function getTransporter(): nodemailer.Transporter { if (!transporter) { transporter = nodemailer.createTransport({ host: SMTP_HOST, port: SMTP_PORT, secure: SMTP_PORT === 465, auth: SMTP_USER ? { user: SMTP_USER, pass: SMTP_PASS } : undefined, }); } return transporter; } export type InquiryEmailData = { id: number; name: string; contact: string; service_title: string; message: string; created_at: string; }; export async function sendInquiryNotification( data: InquiryEmailData ): Promise { if (!SMTP_USER) { throw new Error("SMTP_USER 未配置(.env.local)"); } const rows: [string, string][] = [ ["留言编号", `#${data.id}`], ["姓名", data.name], ["联系方式", data.contact], ["感兴趣的服务", data.service_title || "(未选择)"], ["提交时间", data.created_at], ]; const html = `

新服务咨询 · 北极新一

${rows .map( ([k, v]) => `` ) .join("")}
${k}${escapeHtml( v )}
${escapeHtml( data.message )}

可登录 /virtheart 后台留言箱查看与更新状态。

`; await getTransporter().sendMail({ from: `"通知邮箱" <${SMTP_USER}>`, to: NOTIFY_TO, subject: `【服务咨询 #${data.id}】${data.name} - ${ data.service_title || "未选择服务" }`, text: rows.map(([k, v]) => `${k}:${v}`).join("\n") + `\n\n留言:\n${data.message}`, html, }); } function escapeHtml(s: string) { return s .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); }