feat(email): 添加发件人邮箱字段并优化邮件发送逻辑

添加独立的sender_email字段作为发件人邮箱,优先使用该字段而非smtp_username
更新相关表单、模型和测试用例以支持新字段
重构邮件发送逻辑,统一邮箱格式验证和错误提示
This commit is contained in:
2026-01-18 18:35:09 +08:00
parent 2320133c20
commit e22bd4a8c3
5 changed files with 107 additions and 45 deletions

View File

@@ -63,10 +63,11 @@ def test_email_config():
import re
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# 验证发件人邮箱格式
if config.smtp_username and not re.match(email_pattern, config.smtp_username):
logger.error(f"发件人邮箱格式不正确: {config.smtp_username}")
logger.error("请在系统配置页面输入有效的邮箱地址,而不是手机号或其他格式")
# 验证发件人邮箱格式优先使用sender_email其次使用smtp_username
sender_email = config.sender_email or config.smtp_username
if sender_email and not re.match(email_pattern, sender_email):
logger.error(f"发件人邮箱格式不正确: {sender_email}")
logger.error("请在系统配置页面输入有效的邮箱地址")
return False
# 验证收件人邮箱格式(如果已配置)
@@ -209,9 +210,9 @@ def test_send_simple_email():
# 从数据库获取配置
config = SystemConfig.get_config()
from_email = config.smtp_username or None
from_email = config.sender_email or config.smtp_username or None
if not from_email:
logger.error("未配置发件邮箱 (smtp_username)")
logger.error("未配置发件邮箱")
return False
# 验证发件人邮箱格式
@@ -219,7 +220,7 @@ def test_send_simple_email():
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if not re.match(email_pattern, from_email):
logger.error(f"发件人邮箱格式不正确: {from_email}")
logger.error("请在系统配置页面输入有效的邮箱地址,而不是手机号或其他格式")
logger.error("请在系统配置页面输入有效的邮箱地址")
return False
# 获取收件人(如果没有配置,使用发件人自己)