Spaces:
Sleeping
Sleeping
Commit ·
5863861
1
Parent(s): 374416b
fix: fix email pool auto
Browse files- core/email_generator.py +57 -4
core/email_generator.py
CHANGED
|
@@ -17,6 +17,7 @@ from dataclasses import dataclass, field
|
|
| 17 |
from pathlib import Path
|
| 18 |
|
| 19 |
from dotenv import load_dotenv
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# Name pools for generating realistic emails
|
|
@@ -194,11 +195,11 @@ class EmailGenerator:
|
|
| 194 |
- email@domain.com (uses main IMAP password)
|
| 195 |
- email@domain.com:password (uses specific password for this email)
|
| 196 |
"""
|
| 197 |
-
if not self.config.email_pool:
|
| 198 |
-
raise ValueError("Email pool is empty")
|
| 199 |
-
|
| 200 |
if self._pool_index >= len(self.config.email_pool):
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
entry = self.config.email_pool[self._pool_index]
|
| 204 |
self._pool_index += 1
|
|
@@ -225,6 +226,58 @@ class EmailGenerator:
|
|
| 225 |
display_name=name,
|
| 226 |
imap_password=imap_password
|
| 227 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
def _generate_random_name(self) -> str:
|
| 230 |
"""Generate a random realistic name"""
|
|
|
|
| 17 |
from pathlib import Path
|
| 18 |
|
| 19 |
from dotenv import load_dotenv
|
| 20 |
+
import requests
|
| 21 |
|
| 22 |
|
| 23 |
# Name pools for generating realistic emails
|
|
|
|
| 195 |
- email@domain.com (uses main IMAP password)
|
| 196 |
- email@domain.com:password (uses specific password for this email)
|
| 197 |
"""
|
|
|
|
|
|
|
|
|
|
| 198 |
if self._pool_index >= len(self.config.email_pool):
|
| 199 |
+
if not self._maybe_add_mailapi_address():
|
| 200 |
+
if not self.config.email_pool:
|
| 201 |
+
raise ValueError("Email pool is empty")
|
| 202 |
+
raise ValueError("Email pool exhausted - no more emails available")
|
| 203 |
|
| 204 |
entry = self.config.email_pool[self._pool_index]
|
| 205 |
self._pool_index += 1
|
|
|
|
| 226 |
display_name=name,
|
| 227 |
imap_password=imap_password
|
| 228 |
)
|
| 229 |
+
|
| 230 |
+
def _maybe_add_mailapi_address(self) -> bool:
|
| 231 |
+
backend = os.environ.get('EMAIL_BACKEND', 'imap').strip().lower()
|
| 232 |
+
auto_create = os.environ.get('MAIL_API_AUTO_CREATE', '1').strip().lower()
|
| 233 |
+
if backend != 'mailapi' or auto_create in ('0', 'false', 'no'):
|
| 234 |
+
return False
|
| 235 |
+
address = self._create_mailapi_address()
|
| 236 |
+
if not address:
|
| 237 |
+
return False
|
| 238 |
+
self.config.email_pool.append(address)
|
| 239 |
+
return True
|
| 240 |
+
|
| 241 |
+
def _create_mailapi_address(self) -> Optional[str]:
|
| 242 |
+
base_url = os.environ.get('MAIL_API_BASE_URL', '').strip()
|
| 243 |
+
admin_pwd = os.environ.get('MAIL_API_ADMIN_PWD', '').strip()
|
| 244 |
+
domain = os.environ.get('MAIL_API_DOMAIN', '').strip()
|
| 245 |
+
if not domain:
|
| 246 |
+
domain = os.environ.get('EMAIL_DOMAIN', '').strip()
|
| 247 |
+
timeout = int(os.environ.get('MAIL_API_TIMEOUT', '15'))
|
| 248 |
+
|
| 249 |
+
if not base_url or not admin_pwd or not domain:
|
| 250 |
+
return None
|
| 251 |
+
|
| 252 |
+
name = self._generate_mailapi_name()
|
| 253 |
+
url = base_url.rstrip('/') + '/admin/new_address'
|
| 254 |
+
headers = {
|
| 255 |
+
'x-admin-auth': admin_pwd,
|
| 256 |
+
'Content-Type': 'application/json'
|
| 257 |
+
}
|
| 258 |
+
payload = {
|
| 259 |
+
'enablePrefix': True,
|
| 260 |
+
'name': name,
|
| 261 |
+
'domain': domain
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
try:
|
| 265 |
+
resp = requests.post(url, json=payload, headers=headers, timeout=timeout)
|
| 266 |
+
if resp.status_code != 200:
|
| 267 |
+
return None
|
| 268 |
+
data = resp.json() if resp.content else {}
|
| 269 |
+
address = data.get('address')
|
| 270 |
+
if not address:
|
| 271 |
+
address = f"{name}@{domain}"
|
| 272 |
+
return address
|
| 273 |
+
except Exception:
|
| 274 |
+
return None
|
| 275 |
+
|
| 276 |
+
def _generate_mailapi_name(self) -> str:
|
| 277 |
+
letters1 = ''.join(random.choices(string.ascii_lowercase, k=5))
|
| 278 |
+
numbers = ''.join(random.choices(string.digits, k=random.randint(1, 3)))
|
| 279 |
+
letters2 = ''.join(random.choices(string.ascii_lowercase, k=random.randint(1, 3)))
|
| 280 |
+
return letters1 + numbers + letters2
|
| 281 |
|
| 282 |
def _generate_random_name(self) -> str:
|
| 283 |
"""Generate a random realistic name"""
|