Skip to content

Commit b2d03cf

Browse files
committed
fix: SMTP 465/SSL support + add SMTP config to .env.example and 备忘录
1 parent 17b76c8 commit b2d03cf

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

.env.example

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ TP_SERVER_HOST=0.0.0.0
3030
TP_SERVER_PORT=8900
3131
TP_SERVER_LOG_LEVEL=INFO
3232

33+
# ── 邮件验证码 ──────────────────────────────────
34+
# 注册时发送验证码邮件(不配置则走开发模式,验证码直接返回在 API 响应中)
35+
# REQUIRE_EMAIL_VERIFICATION=true ← 上线前改为 true
36+
REQUIRE_EMAIL_VERIFICATION=false
37+
#
38+
# 企业邮 SMTP(以腾讯企业邮为例)
39+
# SMTP_HOST=smtp.exmail.qq.com # 腾讯企业邮
40+
# SMTP_HOST=smtp.mxhichina.com # 阿里云企业邮
41+
# SMTP_PORT=465 # 465=SSL, 587=TLS/STARTTLS
42+
# SMTP_USER=no-reply@xinzaoai.com
43+
# SMTP_PASS=邮箱密码或授权码
44+
# SMTP_FROM=no-reply@xinzaoai.com # 默认等于 SMTP_USER,可省略
45+
SMTP_HOST=
46+
SMTP_PORT=465
47+
SMTP_USER=
48+
SMTP_PASS=
49+
SMTP_FROM=
50+
3351
# ── 支付(预留) ────────────────────────────────
3452
PAYMENT_WEBHOOK_SECRET=
3553

src/auth/service.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,17 @@ def send_verification_email(email: str, code: str) -> bool:
511511
msg["From"] = from_email
512512
msg["To"] = email
513513
try:
514-
with smtplib.SMTP(smtp_host, smtp_port, timeout=10) as server:
515-
server.starttls()
516-
server.login(smtp_user, smtp_pass)
517-
server.sendmail(from_email, [email], msg.as_string())
514+
if smtp_port == 465:
515+
# 端口 465:直接 SSL
516+
with smtplib.SMTP_SSL(smtp_host, smtp_port, timeout=10) as server:
517+
server.login(smtp_user, smtp_pass)
518+
server.sendmail(from_email, [email], msg.as_string())
519+
else:
520+
# 端口 587:STARTTLS
521+
with smtplib.SMTP(smtp_host, smtp_port, timeout=10) as server:
522+
server.starttls()
523+
server.login(smtp_user, smtp_pass)
524+
server.sendmail(from_email, [email], msg.as_string())
518525
logger.info("验证码邮件已发送 → {}", email)
519526
return True
520527
except Exception as e:

开发备忘录.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,56 @@ sudo systemctl restart nginx
26522652
| 443 | TCP | HTTPS | 腾讯云防火墙 |
26532653
| 3306 | - | MySQL | 阿里云 RDS 白名单添加 82.157.97.179 |
26542654

2655+
### 15.6 邮件验证码(SMTP)配置
2656+
2657+
#### 当前状态
2658+
- **开发模式**`REQUIRE_EMAIL_VERIFICATION=false`):服务器把验证码直接附在 API 响应的 `dev_code` 字段里,插件自动填入,用户无需收邮件。⚠️ 上线前必须开启真实发信。
2659+
- 发件邮箱:`no-reply@xinzaoai.com`(企业邮箱,已注册)
2660+
- 后端代码:`src/auth/service.py → send_verification_email()` 已完整实现,只需配置环境变量。
2661+
2662+
#### 上线前操作(一次性)
2663+
2664+
1. **登录企业邮箱后台**,找到 SMTP 信息(服务器地址、端口、密码/授权码)
2665+
2666+
2. **更新服务器环境变量**(SSH 到服务器后编辑 .env):
2667+
2668+
```bash
2669+
ssh -i key/TestPilotAi.pem ubuntu@82.157.97.179
2670+
sudo nano /opt/testpilot/app/.env
2671+
```
2672+
2673+
在 .env 中加入(以腾讯企业邮为例):
2674+
```
2675+
REQUIRE_EMAIL_VERIFICATION=true
2676+
SMTP_HOST=smtp.exmail.qq.com
2677+
SMTP_PORT=465
2678+
SMTP_USER=no-reply@xinzaoai.com
2679+
SMTP_PASS=你的邮箱密码
2680+
SMTP_FROM=no-reply@xinzaoai.com
2681+
```
2682+
2683+
其他常见企业邮 SMTP 地址:
2684+
| 邮箱服务商 | SMTP_HOST | SMTP_PORT |
2685+
|----------|-----------|-----------|
2686+
| 腾讯企业邮 | smtp.exmail.qq.com | 465(SSL) |
2687+
| 阿里云企业邮 | smtp.mxhichina.com | 465(SSL) |
2688+
| 网易企业邮 | smtp.qiye.163.com | 465(SSL) |
2689+
2690+
3. **重启服务**
2691+
```bash
2692+
sudo systemctl restart testpilot-api
2693+
sudo systemctl status testpilot-api
2694+
```
2695+
2696+
4. **验证**:注册新账号,邮箱收到"【TestPilot AI】注册验证码 XXXXXX"邮件即成功
2697+
2698+
#### 关键技术细节
2699+
- 验证码有效期:10 分钟,使用一次即失效
2700+
- 发送内容:主题 `【TestPilot AI】注册验证码 {code}`,正文纯文本
2701+
- 代码位置:`src/auth/service.py``send_verification_email(email, code)`
2702+
- 使用 `smtplib.SMTP` + `starttls()`(端口587)或先 `smtplib.SMTP_SSL`(端口465)
2703+
- ⚠️ 当前代码对 465/SSL 和 587/TLS 的处理:统一走 `starttls()`,如果企业邮要求 465 纯 SSL,需要改用 `smtplib.SMTP_SSL`
2704+
26552705
---
26562706

26572707
### iOS 真机测试完整验证(2026-03-15)

0 commit comments

Comments
 (0)