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

@@ -59,6 +59,22 @@ def test_email_config():
logger.info(" - SMTP密码: 发件邮箱密码")
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
logger.success("邮件配置测试通过!")
return True
@@ -198,8 +214,21 @@ def test_send_simple_email():
logger.error("未配置发件邮箱 (smtp_username)")
return False
# 验证发件人邮箱格式
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):
logger.error(f"发件人邮箱格式不正确: {from_email}")
logger.error("请在系统配置页面输入有效的邮箱地址,而不是手机号或其他格式")
return False
# 获取收件人(如果没有配置,使用发件人自己)
to_email = config.recipient_email or from_email
# 验证收件人邮箱格式
if not re.match(email_pattern, to_email):
logger.error(f"收件人邮箱格式不正确: {to_email}")
logger.error("请在系统配置页面输入有效的邮箱地址")
return False
recipient_list = [to_email]
# 获取SMTP配置
@@ -304,7 +333,21 @@ def test_send_html_email_with_attachment():
logger.error("未配置发件邮箱")
return False
# 验证发件人邮箱格式
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):
logger.error(f"发件人邮箱格式不正确: {from_email}")
logger.error("请在系统配置页面输入有效的邮箱地址,而不是手机号或其他格式")
return False
# 获取收件人(如果没有配置,使用发件人自己)
to_email = config.recipient_email or from_email
# 验证收件人邮箱格式
if not re.match(email_pattern, to_email):
logger.error(f"收件人邮箱格式不正确: {to_email}")
logger.error("请在系统配置页面输入有效的邮箱地址")
return False
recipient_list = [to_email]
# 获取SMTP配置
@@ -415,7 +458,21 @@ def test_email_performance():
logger.error("未配置发件邮箱")
return False
# 验证发件人邮箱格式
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):
logger.error(f"发件人邮箱格式不正确: {from_email}")
logger.error("请在系统配置页面输入有效的邮箱地址,而不是手机号或其他格式")
return False
# 获取收件人(如果没有配置,使用发件人自己)
to_email = config.recipient_email or from_email
# 验证收件人邮箱格式
if not re.match(email_pattern, to_email):
logger.error(f"收件人邮箱格式不正确: {to_email}")
logger.error("请在系统配置页面输入有效的邮箱地址")
return False
recipient_list = [to_email]
# 获取SMTP配置