From b50ad199dec14646f8e44881dc20965d53c21a94 Mon Sep 17 00:00:00 2001 From: kyx236 Date: Fri, 30 Jan 2026 16:02:00 +0800 Subject: [PATCH] feat(email_service): Add dynamic email provider configuration support - Import EMAIL_PROVIDER from config at runtime in unified functions to enable dynamic provider switching - Update unified_generate_email() to fetch current provider configuration on each call - Update unified_create_email() to fetch current provider configuration on each call - Update unified_get_verification_code() to fetch current provider configuration on each call - Update unified_fetch_emails() to fetch current provider configuration on each call - Update fallback comments to reflect support for both KYX and Cloud Mail systems - Allows email provider to be switched without restarting the application --- email_service.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/email_service.py b/email_service.py index 10d21b7..3445ddb 100644 --- a/email_service.py +++ b/email_service.py @@ -632,7 +632,10 @@ def unified_generate_email() -> str: Returns: str: 邮箱地址 """ - if EMAIL_PROVIDER == "gptmail": + # 每次调用时重新获取配置,支持动态切换 + from config import EMAIL_PROVIDER as current_provider + + if current_provider == "gptmail": # 生成随机前缀 + oaiteam 后缀,确保不重复 random_str = ''.join(random.choices(string.ascii_lowercase + string.digits, k=8)) prefix = f"{random_str}-oaiteam" @@ -642,7 +645,7 @@ def unified_generate_email() -> str: return email log.warning(f"GPTMail 生成失败,回退到 KYX: {error}") - # 默认使用 KYX 系统 + # 默认使用 KYX / Cloud Mail 系统 return generate_random_email() @@ -652,7 +655,10 @@ def unified_create_email() -> tuple[str, str]: Returns: tuple: (email, password) """ - if EMAIL_PROVIDER == "gptmail": + # 每次调用时重新获取配置,支持动态切换 + from config import EMAIL_PROVIDER as current_provider + + if current_provider == "gptmail": # 生成随机前缀 + oaiteam 后缀,确保不重复 random_str = ''.join(random.choices(string.ascii_lowercase + string.digits, k=8)) prefix = f"{random_str}-oaiteam" @@ -663,7 +669,7 @@ def unified_create_email() -> tuple[str, str]: return email, DEFAULT_PASSWORD log.warning(f"GPTMail 生成失败,回退到 KYX: {error}") - # 默认使用 KYX 系统 + # 默认使用 KYX / Cloud Mail 系统 email = generate_random_email() success, msg = create_email_user(email, DEFAULT_PASSWORD) if success or "已存在" in msg: @@ -682,10 +688,13 @@ def unified_get_verification_code(email: str, max_retries: int = None, interval: Returns: tuple: (code, error, email_time) - 验证码、错误信息、邮件时间 """ - if EMAIL_PROVIDER == "gptmail": + # 每次调用时重新获取配置,支持动态切换 + from config import EMAIL_PROVIDER as current_provider + + if current_provider == "gptmail": return gptmail_service.get_verification_code(email, max_retries, interval) - # 默认使用 KYX 系统 + # 默认使用 KYX / Cloud Mail 系统 return get_verification_code(email, max_retries, interval) @@ -698,9 +707,12 @@ def unified_fetch_emails(email: str) -> list: Returns: list: 邮件列表 """ - if EMAIL_PROVIDER == "gptmail": + # 每次调用时重新获取配置,支持动态切换 + from config import EMAIL_PROVIDER as current_provider + + if current_provider == "gptmail": emails, error = gptmail_service.get_emails(email) return emails - # 默认使用 KYX 系统 + # 默认使用 KYX / Cloud Mail 系统 return fetch_email_content(email)