mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 01:40:21 +08:00
refactor: 提取正则工具模块并清理废弃代码
前端: - 新增 model-mapping-regex.ts 工具模块,统一正则验证和 LRU 缓存 - ModelMappingsTab/RoutingTab 重构为使用 computed 缓存匹配结果 - 移除多个组件中未使用的变量和函数 - 添加 HTMLImageElement/HTMLIFrameElement 到 ESLint 全局类型 - 修复 vitest 需要 --experimental-require-module 的问题 后端: - 移除废弃的异步数据库支持 (get_async_db, AsyncSession) - 将 async_utils 从 database/ 迁移到 utils/ - 改进 database.py 类型标注 - 修复 email 模块的 aiosmtplib 可选导入类型问题
This commit is contained in:
@@ -3,20 +3,21 @@
|
||||
提供 SMTP 邮件发送功能
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import smtplib
|
||||
import ssl
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from typing import Optional, Tuple
|
||||
from typing import Any, Optional, Tuple, Union
|
||||
|
||||
aiosmtplib: Any
|
||||
try:
|
||||
import aiosmtplib
|
||||
|
||||
AIOSMTPLIB_AVAILABLE = True
|
||||
import aiosmtplib as _aiosmtplib
|
||||
except ImportError:
|
||||
AIOSMTPLIB_AVAILABLE = False
|
||||
aiosmtplib = None
|
||||
else:
|
||||
AIOSMTPLIB_AVAILABLE = True
|
||||
aiosmtplib = _aiosmtplib
|
||||
|
||||
|
||||
def _create_ssl_context() -> ssl.SSLContext:
|
||||
@@ -34,6 +35,7 @@ from sqlalchemy.orm import Session
|
||||
from src.core.crypto import crypto_service
|
||||
from src.core.logger import logger
|
||||
from src.services.system.config import SystemConfigService
|
||||
from src.utils.async_utils import run_in_executor
|
||||
|
||||
from .email_template import EmailTemplate
|
||||
|
||||
@@ -260,9 +262,13 @@ class EmailSenderService:
|
||||
Returns:
|
||||
(是否发送成功, 错误信息)
|
||||
"""
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(
|
||||
None, EmailSenderService._send_email_sync, config, to_email, subject, html_body, text_body
|
||||
return await run_in_executor(
|
||||
EmailSenderService._send_email_sync,
|
||||
config,
|
||||
to_email,
|
||||
subject,
|
||||
html_body,
|
||||
text_body,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@@ -302,7 +308,7 @@ class EmailSenderService:
|
||||
message.attach(MIMEText(html_body, "html", "utf-8"))
|
||||
|
||||
# 连接 SMTP 服务器
|
||||
server = None
|
||||
server: Optional[smtplib.SMTP] = None
|
||||
ssl_context = _create_ssl_context()
|
||||
try:
|
||||
if config["smtp_use_ssl"]:
|
||||
@@ -321,6 +327,8 @@ class EmailSenderService:
|
||||
if config["smtp_use_tls"]:
|
||||
server.starttls(context=ssl_context)
|
||||
|
||||
assert server is not None
|
||||
|
||||
# 登录
|
||||
if config["smtp_user"] and config["smtp_password"]:
|
||||
server.login(config["smtp_user"], config["smtp_password"])
|
||||
@@ -393,6 +401,7 @@ class EmailSenderService:
|
||||
await smtp.quit()
|
||||
else:
|
||||
# 使用同步方式测试
|
||||
server: Union[smtplib.SMTP, smtplib.SMTP_SSL]
|
||||
if config["smtp_use_ssl"]:
|
||||
server = smtplib.SMTP_SSL(
|
||||
config["smtp_host"],
|
||||
|
||||
Reference in New Issue
Block a user