Files
medical-mall/pages/user/test/EMAIL_CONFIG_FIX.md
2026-01-26 21:34:17 +08:00

122 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 注册邮件发送失败问题修复指南
## 🔍 问题描述
注册时出现错误:`Error sending confirmation email` (500 Internal Server Error)
**原因**
- Supabase 配置了 `ENABLE_EMAIL_AUTOCONFIRM=false`,需要发送确认邮件
- SMTP 配置使用的是假服务(`supabase-mail`, `fake_mail_user`),无法发送邮件
- 当 Supabase 尝试发送邮件时失败,返回 500 错误
## ✅ 解决方案
### 方案一:启用自动确认(推荐,开发环境)
修改 `supabase_pro/.env` 文件:
```env
## Email auth
ENABLE_EMAIL_SIGNUP=true
ENABLE_EMAIL_AUTOCONFIRM=true # 改为 true跳过邮件验证
```
**优点**
- 注册后立即可以登录,无需邮件验证
- 适合开发和测试环境
**缺点**
- 生产环境建议使用真实的邮件服务
---
### 方案二:配置真实的 SMTP 服务
修改 `supabase_pro/.env` 文件,配置真实的 SMTP 服务:
```env
## Email auth
ENABLE_EMAIL_SIGNUP=true
ENABLE_EMAIL_AUTOCONFIRM=false
SMTP_ADMIN_EMAIL=your-admin@example.com
SMTP_HOST=smtp.example.com # 真实的 SMTP 服务器
SMTP_PORT=587 # 或 465 (SSL)
SMTP_USER=your-smtp-user
SMTP_PASS=your-smtp-password
SMTP_SENDER_NAME=Your App Name
```
**常用 SMTP 服务**
- Gmail: `smtp.gmail.com:587`
- 163: `smtp.163.com:465`
- QQ: `smtp.qq.com:587`
- SendGrid, Mailgun 等第三方服务
---
### 方案三:使用 Supabase 本地邮件服务(开发环境)
如果使用 Docker Compose 运行 Supabase可以使用内置的邮件服务
1. 确保 `supabase-mail` 服务正常运行
2. 检查邮件服务日志:
```bash
docker-compose logs supabase-mail
```
3. 如果服务未运行,启动它:
```bash
docker-compose up -d supabase-mail
```
---
## 🔧 已实施的代码修复
已改进注册页面的错误处理:
1. **检查邮件发送失败错误**:如果返回 500 错误且错误信息包含 "confirmation email",会给出友好提示
2. **即使邮件发送失败,用户可能已创建**:提示用户稍后尝试登录
3. **改进错误提示**:更清晰的错误信息
---
## 📝 验证步骤
1. **修改配置后,重启 Supabase**
```bash
cd supabase_pro
docker-compose restart auth
```
2. **测试注册**
- 尝试注册新用户
- 如果使用 `ENABLE_EMAIL_AUTOCONFIRM=true`,应该立即可以登录
- 如果使用真实 SMTP检查邮箱是否收到确认邮件
3. **检查数据库**
```sql
-- 检查 auth.users 表中是否有新用户
SELECT id, email, email_confirmed_at, created_at
FROM auth.users
ORDER BY created_at DESC
LIMIT 5;
-- 检查 ak_users 表中是否有对应记录
SELECT id, email, username, created_at
FROM ak_users
ORDER BY created_at DESC
LIMIT 5;
```
---
## 🎯 推荐配置(开发环境)
```env
## Email auth
ENABLE_EMAIL_SIGNUP=true
ENABLE_EMAIL_AUTOCONFIRM=true # 开发环境跳过邮件验证
```
这样注册后可以立即登录,无需等待邮件确认。