Temp-Mail 邮箱服务 基于自部署 Cloudflare Worker 的临时邮箱,admin 模式管理邮箱 不走代理,不使用 requests 库
| 26 | |
| 27 | |
| 28 | class TempMailService(BaseEmailService): |
| 29 | """ |
| 30 | Temp-Mail 邮箱服务 |
| 31 | 基于自部署 Cloudflare Worker 的临时邮箱,admin 模式管理邮箱 |
| 32 | 不走代理,不使用 requests 库 |
| 33 | """ |
| 34 | |
| 35 | def __init__(self, config: Dict[str, Any] = None, name: str = None): |
| 36 | """ |
| 37 | 初始化 TempMail 服务 |
| 38 | |
| 39 | Args: |
| 40 | config: 配置字典,支持以下键: |
| 41 | - base_url: Worker 域名地址,如 https://mail.example.com (必需) |
| 42 | - admin_password: Admin 密码,对应 x-admin-auth header (必需) |
| 43 | - domain: 邮箱域名,如 example.com (必需) |
| 44 | - enable_prefix: 是否启用前缀,默认 True |
| 45 | - timeout: 请求超时时间,默认 30 |
| 46 | - max_retries: 最大重试次数,默认 3 |
| 47 | name: 服务名称 |
| 48 | """ |
| 49 | super().__init__(EmailServiceType.TEMP_MAIL, name) |
| 50 | |
| 51 | required_keys = ["base_url", "admin_password", "domain"] |
| 52 | missing_keys = [key for key in required_keys if not (config or {}).get(key)] |
| 53 | if missing_keys: |
| 54 | raise ValueError(f"缺少必需配置: {missing_keys}") |
| 55 | |
| 56 | default_config = { |
| 57 | "enable_prefix": True, |
| 58 | "timeout": 30, |
| 59 | "max_retries": 3, |
| 60 | } |
| 61 | self.config = {**default_config, **(config or {})} |
| 62 | |
| 63 | # 不走代理,proxy_url=None |
| 64 | http_config = RequestConfig( |
| 65 | timeout=self.config["timeout"], |
| 66 | max_retries=self.config["max_retries"], |
| 67 | ) |
| 68 | self.http_client = HTTPClient(proxy_url=None, config=http_config) |
| 69 | |
| 70 | # 邮箱缓存:email -> {jwt, address} |
| 71 | self._email_cache: Dict[str, Dict[str, Any]] = {} |
| 72 | # 记录每个邮箱上一次成功使用的邮件 ID,避免重复使用旧验证码 |
| 73 | self._last_used_mail_ids: Dict[str, str] = {} |
| 74 | # /admin/mails 接口对 limit 参数较严格,这里统一限制上限,避免 400 Invalid limit |
| 75 | self._admin_mails_limit_max = 50 |
| 76 | |
| 77 | def _normalize_admin_limit(self, value: Any, default: int = 50) -> int: |
| 78 | try: |
| 79 | number = int(value) |
| 80 | except Exception: |
| 81 | number = int(default) |
| 82 | if number <= 0: |
| 83 | number = int(default) |
| 84 | return max(1, min(number, int(self._admin_mails_limit_max))) |
| 85 |
no outgoing calls