- 新增 nodemailer 依赖,通过腾讯企业邮 SMTP 发送邮件 - 新建 server/utils/email.js 邮件发送工具 - 定制表单提交后企业微信和邮件通知并行发送 - 不配置 SMTP 环境变量时静默跳过,不影响原有流程 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
72 行
2.1 KiB
JavaScript
72 行
2.1 KiB
JavaScript
/**
|
||
* 邮件通知工具
|
||
* 通过 SMTP 发送邮件通知(腾讯企业邮)
|
||
*
|
||
* 环境变量:
|
||
* SMTP_HOST - SMTP 服务器地址(默认 smtp.exmail.qq.com)
|
||
* SMTP_PORT - SMTP 端口(默认 465)
|
||
* SMTP_USER - 发件邮箱账号
|
||
* SMTP_PASS - 发件邮箱密码 / 授权码
|
||
* NOTIFY_EMAIL - 收件邮箱(默认 lt@1814.love)
|
||
*
|
||
* 不配置 SMTP_USER / SMTP_PASS 则静默跳过
|
||
*/
|
||
|
||
import { createTransport } from 'nodemailer'
|
||
|
||
let transporter = null
|
||
|
||
function getTransporter() {
|
||
if (transporter) return transporter
|
||
|
||
const user = process.env.SMTP_USER
|
||
const pass = process.env.SMTP_PASS
|
||
if (!user || !pass) return null
|
||
|
||
transporter = createTransport({
|
||
host: process.env.SMTP_HOST || 'smtp.exmail.qq.com',
|
||
port: Number(process.env.SMTP_PORT) || 465,
|
||
secure: true,
|
||
auth: { user, pass },
|
||
})
|
||
|
||
return transporter
|
||
}
|
||
|
||
/**
|
||
* 发送邮件通知
|
||
* @param {string} subject - 邮件主题
|
||
* @param {Record<string, string>} fields - { 字段名: 字段值 }
|
||
*/
|
||
export async function notifyEmail(subject, fields) {
|
||
const t = getTransporter()
|
||
if (!t) return
|
||
|
||
const rows = Object.entries(fields)
|
||
.map(([k, v]) => `<tr><td style="padding:8px 12px;border:1px solid #e5e7eb;font-weight:600;white-space:nowrap;background:#f9fafb;">${k}</td><td style="padding:8px 12px;border:1px solid #e5e7eb;">${v}</td></tr>`)
|
||
.join('')
|
||
|
||
const now = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })
|
||
|
||
const html = `
|
||
<div style="font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;max-width:600px;margin:0 auto;">
|
||
<h2 style="color:#3a7d44;border-bottom:2px solid #3a7d44;padding-bottom:8px;">${subject}</h2>
|
||
<table style="width:100%;border-collapse:collapse;font-size:14px;">${rows}</table>
|
||
<p style="color:#9ca3af;font-size:12px;margin-top:16px;">提交时间:${now}</p>
|
||
</div>
|
||
`
|
||
|
||
const to = process.env.NOTIFY_EMAIL || 'lt@1814.love'
|
||
|
||
try {
|
||
await t.sendMail({
|
||
from: `"呼籁旅行" <${process.env.SMTP_USER}>`,
|
||
to,
|
||
subject,
|
||
html,
|
||
})
|
||
} catch (err) {
|
||
console.error('[email] 邮件发送失败:', err?.message)
|
||
}
|
||
}
|