feat(邮件验证): 添加发件人和收件人邮箱格式验证
在测试文件中添加邮箱格式验证逻辑,使用正则表达式检查邮箱格式是否正确 当邮箱格式无效时,记录错误日志并返回验证失败
This commit is contained in:
50
logs/app.log
50
logs/app.log
@@ -48,3 +48,53 @@
|
|||||||
2026-01-18 16:23:56.513 | INFO | __main__:main:560 - 3. ❌ 检查网络连接
|
2026-01-18 16:23:56.513 | INFO | __main__:main:560 - 3. ❌ 检查网络连接
|
||||||
2026-01-18 16:23:56.513 | INFO | __main__:main:561 - 4. 参考README中的邮件配置章节
|
2026-01-18 16:23:56.513 | INFO | __main__:main:561 - 4. 参考README中的邮件配置章节
|
||||||
2026-01-18 16:23:56.513 | INFO | __main__:main:562 - 5. 查看详细错误日志
|
2026-01-18 16:23:56.513 | INFO | __main__:main:562 - 5. 查看详细错误日志
|
||||||
|
2026-01-18 18:02:52.470 | INFO | __main__:test_email_config:39 - 邮件配置信息:
|
||||||
|
2026-01-18 18:02:52.471 | WARNING | __main__:test_email_config:42 - smtp_server: 未配置
|
||||||
|
2026-01-18 18:02:52.471 | INFO | __main__:test_email_config:44 - smtp_port: 587
|
||||||
|
2026-01-18 18:02:52.471 | WARNING | __main__:test_email_config:42 - smtp_username: 未配置
|
||||||
|
2026-01-18 18:02:52.471 | WARNING | __main__:test_email_config:42 - smtp_password: 未配置
|
||||||
|
2026-01-18 18:02:52.472 | WARNING | __main__:test_email_config:42 - recipient_email: 未配置
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:test_email_config:44 - send_time: 08:00:00
|
||||||
|
2026-01-18 18:02:52.472 | ERROR | __main__:test_email_config:54 - 缺少必要的邮件配置: smtp_server, smtp_username, smtp_password
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:test_email_config:55 - 请在系统配置页面配置以下参数:
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:test_email_config:56 - - SMTP服务器: SMTP服务器地址
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:test_email_config:57 - - SMTP端口: SMTP端口(通常是587或465)
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:test_email_config:58 - - SMTP用户名: 发件邮箱
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:test_email_config:59 - - SMTP密码: 发件邮箱密码
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:main:551 -
|
||||||
|
[测试2] SMTP连接测试
|
||||||
|
2026-01-18 18:02:52.472 | INFO | __main__:test_smtp_connection:72 - 开始测试SMTP连接...
|
||||||
|
2026-01-18 18:02:52.475 | INFO | __main__:test_smtp_connection:92 - 连接SMTP服务器: localhost:587
|
||||||
|
2026-01-18 18:02:52.475 | INFO | __main__:test_smtp_connection:93 - 使用TLS: True, 使用SSL: False
|
||||||
|
2026-01-18 18:02:52.475 | INFO | __main__:test_smtp_connection:108 - 正在建立SMTP连接...
|
||||||
|
2026-01-18 18:02:57.860 | ERROR | __main__:test_smtp_connection:131 - SMTP连接测试失败: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
|
||||||
|
2026-01-18 18:02:57.861 | ERROR | __main__:test_smtp_connection:171 - 请检查:
|
||||||
|
2026-01-18 18:02:57.861 | ERROR | __main__:test_smtp_connection:172 - 1. SMTP服务器配置是否正确
|
||||||
|
2026-01-18 18:02:57.862 | ERROR | __main__:test_smtp_connection:173 - 2. 邮箱账号和密码是否正确
|
||||||
|
2026-01-18 18:02:57.862 | ERROR | __main__:test_smtp_connection:174 - 3. 网络连接是否正常
|
||||||
|
2026-01-18 18:02:57.863 | ERROR | __main__:test_smtp_connection:175 - 4. 防火墙是否允许出站连接
|
||||||
|
2026-01-18 18:02:57.863 | INFO | __main__:main:556 -
|
||||||
|
[测试3] 发送简单文本邮件
|
||||||
|
2026-01-18 18:02:57.864 | INFO | __main__:test_send_simple_email:182 - 开始测试发送简单邮件...
|
||||||
|
2026-01-18 18:02:57.865 | ERROR | __main__:test_send_simple_email:198 - 未配置发件邮箱 (smtp_username)
|
||||||
|
2026-01-18 18:02:57.866 | INFO | __main__:main:561 -
|
||||||
|
[测试4] 发送HTML邮件(带附件)
|
||||||
|
2026-01-18 18:02:57.866 | INFO | __main__:test_send_html_email_with_attachment:301 - 开始测试发送HTML邮件(带附件)...
|
||||||
|
2026-01-18 18:02:57.868 | ERROR | __main__:test_send_html_email_with_attachment:317 - 未配置发件邮箱
|
||||||
|
2026-01-18 18:02:57.869 | INFO | __main__:main:566 -
|
||||||
|
[测试5] 邮件发送性能测试
|
||||||
|
2026-01-18 18:02:57.869 | INFO | __main__:test_email_performance:425 - 开始测试邮件发送性能...
|
||||||
|
2026-01-18 18:02:57.871 | ERROR | __main__:test_email_performance:442 - 未配置发件邮箱
|
||||||
|
2026-01-18 18:02:57.871 | INFO | __main__:main:571 -
|
||||||
|
============================================================
|
||||||
|
2026-01-18 18:02:57.872 | INFO | __main__:main:572 - 测试总结:
|
||||||
|
2026-01-18 18:02:57.872 | INFO | __main__:main:573 - 通过测试: 0/5
|
||||||
|
2026-01-18 18:02:57.872 | INFO | __main__:main:574 - ============================================================
|
||||||
|
2026-01-18 18:02:57.873 | ERROR | __main__:main:597 - 多数测试失败,邮件系统无法正常工作。
|
||||||
|
2026-01-18 18:02:57.873 | INFO | __main__:main:598 -
|
||||||
|
紧急处理:
|
||||||
|
2026-01-18 18:02:57.873 | INFO | __main__:main:599 - 1. ❌ 检查SMTP服务器配置
|
||||||
|
2026-01-18 18:02:57.873 | INFO | __main__:main:600 - 2. ❌ 验证邮箱账号和密码
|
||||||
|
2026-01-18 18:02:57.874 | INFO | __main__:main:601 - 3. ❌ 检查网络连接
|
||||||
|
2026-01-18 18:02:57.874 | INFO | __main__:main:602 - 4. 参考README中的邮件配置章节
|
||||||
|
2026-01-18 18:02:57.874 | INFO | __main__:main:603 - 5. 查看详细错误日志
|
||||||
|
|||||||
@@ -77,6 +77,22 @@ def test_celery_email_config():
|
|||||||
logger.error(f"缺少必要的配置: {', '.join(missing_configs)}")
|
logger.error(f"缺少必要的配置: {', '.join(missing_configs)}")
|
||||||
return False
|
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连接
|
# 测试Celery连接
|
||||||
logger.info("测试Celery连接...")
|
logger.info("测试Celery连接...")
|
||||||
try:
|
try:
|
||||||
@@ -188,7 +204,16 @@ def celery_send_test_email(self, test_mode=True):
|
|||||||
if not from_email:
|
if not from_email:
|
||||||
raise ValueError("未配置发件邮箱 (smtp_username)")
|
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
|
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]
|
recipient_list = [to_email]
|
||||||
|
|
||||||
# 获取SMTP配置
|
# 获取SMTP配置
|
||||||
@@ -321,7 +346,16 @@ def celery_send_html_report_email(self, include_attachment=False):
|
|||||||
if not from_email:
|
if not from_email:
|
||||||
raise ValueError("未配置发件邮箱")
|
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
|
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):
|
if isinstance(to_email, list):
|
||||||
recipient_list = to_email
|
recipient_list = to_email
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -59,6 +59,22 @@ def test_email_config():
|
|||||||
logger.info(" - SMTP密码: 发件邮箱密码")
|
logger.info(" - SMTP密码: 发件邮箱密码")
|
||||||
return False
|
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("邮件配置测试通过!")
|
logger.success("邮件配置测试通过!")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -198,8 +214,21 @@ def test_send_simple_email():
|
|||||||
logger.error("未配置发件邮箱 (smtp_username)")
|
logger.error("未配置发件邮箱 (smtp_username)")
|
||||||
return False
|
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
|
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]
|
recipient_list = [to_email]
|
||||||
|
|
||||||
# 获取SMTP配置
|
# 获取SMTP配置
|
||||||
@@ -304,7 +333,21 @@ def test_send_html_email_with_attachment():
|
|||||||
logger.error("未配置发件邮箱")
|
logger.error("未配置发件邮箱")
|
||||||
return False
|
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
|
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]
|
recipient_list = [to_email]
|
||||||
|
|
||||||
# 获取SMTP配置
|
# 获取SMTP配置
|
||||||
@@ -415,7 +458,21 @@ def test_email_performance():
|
|||||||
logger.error("未配置发件邮箱")
|
logger.error("未配置发件邮箱")
|
||||||
return False
|
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
|
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]
|
recipient_list = [to_email]
|
||||||
|
|
||||||
# 获取SMTP配置
|
# 获取SMTP配置
|
||||||
|
|||||||
Reference in New Issue
Block a user