feat(邮件验证): 添加发件人和收件人邮箱格式验证

在测试文件中添加邮箱格式验证逻辑,使用正则表达式检查邮箱格式是否正确
当邮箱格式无效时,记录错误日志并返回验证失败
This commit is contained in:
2026-01-18 18:05:21 +08:00
parent 9215de5a3d
commit 2320133c20
3 changed files with 141 additions and 0 deletions

View File

@@ -77,6 +77,22 @@ def test_celery_email_config():
logger.error(f"缺少必要的配置: {', '.join(missing_configs)}")
return False
# 验证邮箱格式
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("请在系统配置页面输入有效的邮箱地址")
return False
# 验证收件人邮箱格式(如果已配置)
if config.recipient_email and not re.match(email_pattern, config.recipient_email):
logger.error(f"收件人邮箱格式不正确: {config.recipient_email}")
logger.error("请在系统配置页面输入有效的邮箱地址")
return False
# 测试Celery连接
logger.info("测试Celery连接...")
try:
@@ -188,7 +204,16 @@ def celery_send_test_email(self, test_mode=True):
if not from_email:
raise ValueError("未配置发件邮箱 (smtp_username)")
# 验证发件人邮箱格式
import re
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if not re.match(email_pattern, from_email):
raise ValueError(f"发件人邮箱格式不正确: {from_email}")
to_email = config.recipient_email or from_email
# 验证收件人邮箱格式
if not re.match(email_pattern, to_email):
raise ValueError(f"收件人邮箱格式不正确: {to_email}")
recipient_list = [to_email]
# 获取SMTP配置
@@ -321,7 +346,16 @@ def celery_send_html_report_email(self, include_attachment=False):
if not from_email:
raise ValueError("未配置发件邮箱")
# 验证发件人邮箱格式
import re
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if not re.match(email_pattern, from_email):
raise ValueError(f"发件人邮箱格式不正确: {from_email}")
to_email = config.recipient_email or from_email
# 验证收件人邮箱格式
if not re.match(email_pattern, to_email):
raise ValueError(f"收件人邮箱格式不正确: {to_email}")
if isinstance(to_email, list):
recipient_list = to_email
else: