import nodemailer from 'nodemailer'; // 邮箱配置(从环境变量读取) const SMTP_HOST = process.env.SMTP_HOST || 'smtp.qq.com'; const SMTP_PORT = parseInt(process.env.SMTP_PORT || '465'); const SMTP_USER = process.env.SMTP_USER || ''; const SMTP_PASS = process.env.SMTP_PASS || ''; // SMTP 配置 const transporter = nodemailer.createTransport({ host: SMTP_HOST, port: SMTP_PORT, secure: true, auth: { user: SMTP_USER, pass: SMTP_PASS, }, }); // 邮件模板类型 type EmailType = 'register' | 'login' | 'reset'; // 获取邮件主题 function getSubject(type: EmailType): string { const subjects: Record = { register: 'LionCode - 注册验证码', login: 'LionCode - 登录验证码', reset: 'LionCode - 重置密码验证码', }; return subjects[type]; } // 获取邮件内容 function getEmailContent(type: EmailType, code: string): string { const typeText: Record = { register: '注册账号', login: '登录账号', reset: '重置密码', }; return ` 验证码

LionCode

智能 AI 对话助手

您好!

您正在进行${typeText[type]}操作,验证码如下:

${code}

验证码有效期为 5 分钟,请尽快完成验证。

如果这不是您本人的操作,请忽略此邮件。

此邮件由系统自动发送,请勿回复。

© ${new Date().getFullYear()} LionCode. All rights reserved.

`.trim(); } // 发送验证码邮件 export async function sendVerificationEmail( to: string, code: string, type: EmailType ): Promise<{ success: boolean; error?: string }> { try { await transporter.sendMail({ from: { name: 'LionCode', address: SMTP_USER, }, to, subject: getSubject(type), html: getEmailContent(type, code), }); return { success: true }; } catch (error) { console.error('发送邮件失败:', error); return { success: false, error: error instanceof Error ? error.message : '发送邮件失败', }; } } // 生成6位数字验证码 export function generateVerificationCode(): string { return Math.floor(100000 + Math.random() * 900000).toString(); }