feat(邮件通知): 添加测试邮件发送功能

在邮件通知设置页面添加发送测试邮件按钮,用于验证SMTP配置是否正确。同时添加QQ邮箱授权码的使用提示,帮助用户正确配置。

- 新增测试邮件API接口
- 前端添加测试邮件按钮及相关逻辑
- 优化错误提示,特别是针对QQ邮箱的535错误
- 添加QQ邮箱授权码使用说明文档
This commit is contained in:
anghunk
2026-01-20 12:38:48 +08:00
parent 6d3bdb2b11
commit 1c9e8c02b6
5 changed files with 165 additions and 1 deletions

View File

@@ -94,6 +94,47 @@ async function dispatchMail(
}
}
export async function sendTestEmail(
env: Bindings,
to: string,
smtp: EmailNotificationSettings['smtp']
): Promise<{ success: boolean; message?: string }> {
if (!smtp || !smtp.user || !smtp.pass) {
return { success: false, message: 'SMTP 配置不完整' };
}
try {
const transporter = createTransport({
host: smtp.host,
port: smtp.port,
secure: smtp.secure,
auth: { user: smtp.user, pass: smtp.pass }
});
// 尝试验证连接配置
await transporter.verify();
await transporter.sendMail({
from: `"Test" <${smtp.user}>`,
to: to,
subject: 'CWD Comments 邮件配置测试',
html: `
<div style="padding: 20px; font-family: sans-serif;">
<h2 style="color: #059669;">配置成功!</h2>
<p>这就是一封来自 CWD Comments 的测试邮件。</p>
<p>如果您收到了这封邮件,说明您的 SMTP 配置是正确的。</p>
<hr style="border: none; border-top: 1px solid #eee; margin: 20px 0;">
<p style="font-size: 12px; color: #666;">发送时间:${new Date().toLocaleString()}</p>
</div>
`
});
return { success: true };
} catch (e: any) {
console.error('TestEmail:error', e);
return { success: false, message: e.message || String(e) };
}
}
function parseEnabled(raw: string | undefined, defaultValue: boolean) {
if (raw === undefined) return defaultValue;
return raw === '1';