import nodemailer from 'nodemailer' import { useRuntimeConfig } from '#imports' interface MailOptions { to?: string subject: string text: string html?: string } /** * 发送邮件。SMTP 未配置时仅记录日志(数据已入库,不阻塞业务)。 * 环境变量: NUXT_SMTP_HOST / NUXT_SMTP_PORT / NUXT_SMTP_USER / NUXT_SMTP_PASS * NUXT_SMTP_FROM / NUXT_SMTP_CONTACT_TO */ export async function sendMail({ to, subject, text, html }: MailOptions): Promise { const config = useRuntimeConfig() const smtp = config.smtp as { host?: string; port?: number; user?: string; pass?: string; from?: string; contactTo?: string } if (!smtp?.host || !smtp?.user || !smtp?.pass) { console.log(`[mail] SMTP 未配置,跳过发送。主题: ${subject},收件人: ${to || smtp?.contactTo || '(未配置)'}`) return } const port = Number(smtp.port) || 465 const transporter = nodemailer.createTransport({ host: smtp.host, port, secure: port === 465, auth: { user: smtp.user, pass: smtp.pass }, }) await transporter.sendMail({ from: smtp.from || smtp.user, to: to || smtp.contactTo || smtp.user, subject, text, html, }) console.log(`[mail] 邮件已发送: ${subject} -> ${to || smtp.contactTo}`) }