refactor(email): 重构邮件配置从settings.py迁移到数据库
将邮件配置从Django的settings.py迁移到数据库的SystemConfig模型 更新测试文件以使用新的配置方式 添加邮件后端连接配置以提高灵活性
This commit is contained in:
168
test_email.py
168
test_email.py
@@ -21,19 +21,19 @@ def test_email_config():
|
||||
django.setup()
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import get_connection
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from core.models import SystemConfig
|
||||
|
||||
# 从数据库获取系统配置
|
||||
config = SystemConfig.get_config()
|
||||
|
||||
# 检查邮件配置
|
||||
email_config = {
|
||||
'EMAIL_BACKEND': getattr(settings, 'EMAIL_BACKEND', None),
|
||||
'EMAIL_HOST': getattr(settings, 'EMAIL_HOST', None),
|
||||
'EMAIL_PORT': getattr(settings, 'EMAIL_PORT', None),
|
||||
'EMAIL_USE_TLS': getattr(settings, 'EMAIL_USE_TLS', None),
|
||||
'EMAIL_USE_SSL': getattr(settings, 'EMAIL_USE_SSL', None),
|
||||
'EMAIL_HOST_USER': getattr(settings, 'EMAIL_HOST_USER', None),
|
||||
'EMAIL_HOST_PASSWORD': '***' if getattr(settings, 'EMAIL_HOST_PASSWORD', None) else None,
|
||||
'smtp_server': config.smtp_server,
|
||||
'smtp_port': config.smtp_port,
|
||||
'smtp_username': config.smtp_username,
|
||||
'smtp_password': '***' if config.smtp_password else None,
|
||||
'recipient_email': config.recipient_email,
|
||||
'send_time': config.send_time,
|
||||
}
|
||||
|
||||
logger.info("邮件配置信息:")
|
||||
@@ -44,16 +44,19 @@ def test_email_config():
|
||||
logger.info(f" {key}: {value}")
|
||||
|
||||
# 验证必要配置
|
||||
required_configs = ['EMAIL_HOST', 'EMAIL_PORT', 'EMAIL_HOST_USER']
|
||||
missing_configs = [cfg for cfg in required_configs if not getattr(settings, cfg, None)]
|
||||
required_configs = ['smtp_server', 'smtp_port', 'smtp_username', 'smtp_password']
|
||||
missing_configs = []
|
||||
for cfg in required_configs:
|
||||
if not getattr(config, cfg, None):
|
||||
missing_configs.append(cfg)
|
||||
|
||||
if missing_configs:
|
||||
logger.error(f"缺少必要的邮件配置: {', '.join(missing_configs)}")
|
||||
logger.info("请在系统配置页面或settings.py中配置以下参数:")
|
||||
logger.info(" - EMAIL_HOST: SMTP服务器地址")
|
||||
logger.info(" - EMAIL_PORT: SMTP端口(通常是587或465)")
|
||||
logger.info(" - EMAIL_HOST_USER: 发件邮箱")
|
||||
logger.info(" - EMAIL_HOST_PASSWORD: 发件邮箱密码")
|
||||
logger.info("请在系统配置页面配置以下参数:")
|
||||
logger.info(" - SMTP服务器: SMTP服务器地址")
|
||||
logger.info(" - SMTP端口: SMTP端口(通常是587或465)")
|
||||
logger.info(" - SMTP用户名: 发件邮箱")
|
||||
logger.info(" - SMTP密码: 发件邮箱密码")
|
||||
return False
|
||||
|
||||
logger.success("邮件配置测试通过!")
|
||||
@@ -73,17 +76,17 @@ def test_smtp_connection():
|
||||
import django
|
||||
django.setup()
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import get_connection
|
||||
from django.core.mail.backends.smtp import EmailBackend
|
||||
from core.models import SystemConfig
|
||||
|
||||
# 获取SMTP配置
|
||||
host = getattr(settings, 'EMAIL_HOST', 'localhost')
|
||||
port = getattr(settings, 'EMAIL_PORT', 587)
|
||||
username = getattr(settings, 'EMAIL_HOST_USER', '')
|
||||
password = getattr(settings, 'EMAIL_HOST_PASSWORD', '')
|
||||
use_tls = getattr(settings, 'EMAIL_USE_TLS', True)
|
||||
use_ssl = getattr(settings, 'EMAIL_USE_SSL', False)
|
||||
# 从数据库获取SMTP配置
|
||||
config = SystemConfig.get_config()
|
||||
host = config.smtp_server or 'localhost'
|
||||
port = config.smtp_port or 587
|
||||
username = config.smtp_username or ''
|
||||
password = config.smtp_password or ''
|
||||
use_tls = True # 默认使用TLS
|
||||
use_ssl = False # 默认不使用SSL
|
||||
timeout = 10 # 连接超时时间
|
||||
|
||||
logger.info(f"连接SMTP服务器: {host}:{port}")
|
||||
@@ -183,29 +186,28 @@ def test_send_simple_email():
|
||||
import django
|
||||
django.setup()
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.utils import timezone
|
||||
from core.models import SystemConfig
|
||||
from django.core.mail.backends.smtp import EmailBackend
|
||||
|
||||
# 获取配置
|
||||
from_email = getattr(settings, 'EMAIL_HOST_USER', None)
|
||||
# 从数据库获取配置
|
||||
config = SystemConfig.get_config()
|
||||
from_email = config.smtp_username or None
|
||||
if not from_email:
|
||||
logger.error("未配置发件邮箱 (EMAIL_HOST_USER)")
|
||||
logger.error("未配置发件邮箱 (smtp_username)")
|
||||
return False
|
||||
|
||||
# 获取收件人(如果没有配置,使用发件人自己)
|
||||
to_email = getattr(settings, 'EMAIL_HOST_USER', from_email)
|
||||
if isinstance(to_email, list):
|
||||
recipient_list = to_email
|
||||
else:
|
||||
recipient_list = [to_email]
|
||||
to_email = config.recipient_email or from_email
|
||||
recipient_list = [to_email]
|
||||
|
||||
# 获取SMTP配置
|
||||
host = getattr(settings, 'EMAIL_HOST', 'localhost')
|
||||
port = getattr(settings, 'EMAIL_PORT', 587)
|
||||
username = getattr(settings, 'EMAIL_HOST_USER', '')
|
||||
password = getattr(settings, 'EMAIL_HOST_PASSWORD', '')
|
||||
use_tls = getattr(settings, 'EMAIL_USE_TLS', True)
|
||||
host = config.smtp_server or 'localhost'
|
||||
port = config.smtp_port or 587
|
||||
username = config.smtp_username or ''
|
||||
password = config.smtp_password or ''
|
||||
use_tls = True # 默认使用TLS
|
||||
|
||||
# 创建测试邮件
|
||||
subject = f"家庭日报系统测试邮件 - {timezone.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
||||
@@ -228,12 +230,23 @@ def test_send_simple_email():
|
||||
logger.info(f" 收件人: {recipient_list}")
|
||||
logger.info(f" 主题: {subject}")
|
||||
|
||||
# 创建邮件后端
|
||||
backend = EmailBackend(
|
||||
host=host,
|
||||
port=port,
|
||||
username=username,
|
||||
password=password,
|
||||
use_tls=use_tls,
|
||||
fail_silently=False
|
||||
)
|
||||
|
||||
# 创建邮件
|
||||
email = EmailMessage(
|
||||
subject=subject,
|
||||
body=body,
|
||||
from_email=from_email,
|
||||
to=recipient_list,
|
||||
connection=backend
|
||||
)
|
||||
email.content_subtype = 'plain'
|
||||
email.encoding = 'utf-8'
|
||||
@@ -279,18 +292,27 @@ def test_send_html_email_with_attachment():
|
||||
import django
|
||||
django.setup()
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils import timezone
|
||||
from core.models import SystemConfig
|
||||
from django.core.mail.backends.smtp import EmailBackend
|
||||
|
||||
# 获取配置
|
||||
from_email = getattr(settings, 'EMAIL_HOST_USER', None)
|
||||
to_email = getattr(settings, 'EMAIL_HOST_USER', from_email)
|
||||
if isinstance(to_email, list):
|
||||
recipient_list = to_email
|
||||
else:
|
||||
recipient_list = [to_email]
|
||||
# 从数据库获取配置
|
||||
config = SystemConfig.get_config()
|
||||
from_email = config.smtp_username or None
|
||||
if not from_email:
|
||||
logger.error("未配置发件邮箱")
|
||||
return False
|
||||
|
||||
to_email = config.recipient_email or from_email
|
||||
recipient_list = [to_email]
|
||||
|
||||
# 获取SMTP配置
|
||||
host = config.smtp_server or 'localhost'
|
||||
port = config.smtp_port or 587
|
||||
username = config.smtp_username or ''
|
||||
password = config.smtp_password or ''
|
||||
use_tls = True # 默认使用TLS
|
||||
|
||||
# 创建HTML内容
|
||||
html_content = f"""
|
||||
@@ -328,12 +350,23 @@ def test_send_html_email_with_attachment():
|
||||
|
||||
subject = f"家庭日报系统 HTML测试邮件 - {timezone.now().strftime('%Y-%m-%d')}"
|
||||
|
||||
# 创建邮件后端
|
||||
backend = EmailBackend(
|
||||
host=host,
|
||||
port=port,
|
||||
username=username,
|
||||
password=password,
|
||||
use_tls=use_tls,
|
||||
fail_silently=False
|
||||
)
|
||||
|
||||
# 创建邮件
|
||||
email = EmailMessage(
|
||||
subject=subject,
|
||||
body=html_content,
|
||||
from_email=from_email,
|
||||
to=recipient_list,
|
||||
connection=backend
|
||||
)
|
||||
email.content_subtype = 'html'
|
||||
email.encoding = 'utf-8'
|
||||
@@ -369,18 +402,38 @@ def test_email_performance():
|
||||
import django
|
||||
django.setup()
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.utils import timezone
|
||||
from core.models import SystemConfig
|
||||
from django.core.mail.backends.smtp import EmailBackend
|
||||
import time
|
||||
|
||||
# 获取配置
|
||||
from_email = getattr(settings, 'EMAIL_HOST_USER', None)
|
||||
to_email = getattr(settings, 'EMAIL_HOST_USER', from_email)
|
||||
if isinstance(to_email, list):
|
||||
recipient_list = to_email
|
||||
else:
|
||||
recipient_list = [to_email]
|
||||
# 从数据库获取配置
|
||||
config = SystemConfig.get_config()
|
||||
from_email = config.smtp_username or None
|
||||
if not from_email:
|
||||
logger.error("未配置发件邮箱")
|
||||
return False
|
||||
|
||||
to_email = config.recipient_email or from_email
|
||||
recipient_list = [to_email]
|
||||
|
||||
# 获取SMTP配置
|
||||
host = config.smtp_server or 'localhost'
|
||||
port = config.smtp_port or 587
|
||||
username = config.smtp_username or ''
|
||||
password = config.smtp_password or ''
|
||||
use_tls = True # 默认使用TLS
|
||||
|
||||
# 创建邮件后端
|
||||
backend = EmailBackend(
|
||||
host=host,
|
||||
port=port,
|
||||
username=username,
|
||||
password=password,
|
||||
use_tls=use_tls,
|
||||
fail_silently=False
|
||||
)
|
||||
|
||||
# 性能测试
|
||||
test_count = 3
|
||||
@@ -395,6 +448,7 @@ def test_email_performance():
|
||||
body=body,
|
||||
from_email=from_email,
|
||||
to=recipient_list,
|
||||
connection=backend
|
||||
)
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
Reference in New Issue
Block a user