4bef241544
- server/api: 注册/登录/登出/会话恢复(JWT + httpOnly Cookie, bcrypt 密码哈希) - server/api: 联系表单与资讯订阅入库,支持 SMTP 邮件通知(可选) - 应用启动自动建表(幂等),docker-compose 新增 PostgreSQL 16 服务 - 前端移除全部 mock:user store、登录注册弹窗、联系表单、页脚订阅接入真实接口 - i18n 补充服务端错误码中英文案,移除 demoHint - deploy: Deployment 环境变量(Secret 引用)、可选集群内 PostgreSQL(postgres.yaml) - 新增 .env.example 与 README/deploy 文档更新
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
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<void> {
|
|
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}`)
|
|
}
|