import { Resend } from "resend"; import type { Env } from "../env"; /** * 发送密码重置邮件 * @param env 环境变量 * @param to 收件人邮箱 * @param resetToken 重置令牌 * @param username 用户名 * @returns 发送结果 */ export async function sendPasswordResetEmail( env: Env, to: string, resetToken: string, username: string ): Promise<{ success: boolean; error?: string }> { const apiKey = env.RESEND_API_KEY; const fromEmail = env.RESEND_FROM_EMAIL || "noreply@yourdomain.com"; if (!apiKey) { return { success: false, error: "RESEND_API_KEY 未配置,请运行: npx wrangler secret put RESEND_API_KEY", }; } try { const resend = new Resend(apiKey); // 发送邮件 const { data, error } = await resend.emails.send({ from: fromEmail, to: [to], subject: "密码重置验证码 - 聊天室", html: `

🔐 密码重置验证码

你好,${username}

我们收到了你的密码重置请求。请使用以下验证码来重置你的密码:

验证码
${resetToken}

请在密码重置页面输入此验证码

⚠️ 重要提示:
  • 此验证码将在 30 分钟后失效
  • 如果你没有请求重置密码,请忽略此邮件
  • 请勿将此验证码分享给他人
`, text: ` 你好,${username}! 我们收到了你的密码重置请求。请使用以下验证码来重置你的密码: 验证码:${resetToken} 重要提示: - 此验证码将在 30 分钟后失效 - 如果你没有请求重置密码,请忽略此邮件 - 请勿将此验证码分享给他人 此邮件由系统自动发送,请勿回复。 `.trim(), }); if (error) { console.error("Resend 发送邮件失败:", error); return { success: false, error: error.message || "邮件发送失败", }; } console.log("密码重置邮件已发送:", data); return { success: true }; } catch (error) { console.error("发送邮件时出错:", error); return { success: false, error: error instanceof Error ? error.message : "未知错误", }; } }