53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
"""
|
|
Email configuration parser and utilities.
|
|
Parses EMAIL_CONFIG environment variable in format:
|
|
smtp://<user>@kitsunehosting.net:<password>@smtp.forwardemail.net:465
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
from urllib.parse import urlparse, unquote
|
|
|
|
|
|
def parse_email_config(email_config_str):
|
|
"""
|
|
Parse EMAIL_CONFIG string into email settings.
|
|
|
|
Format: smtp://<user>@kitsunehosting.net:<password>@smtp.forwardemail.net:465
|
|
|
|
Returns dict with: user, password, host, port, from_email
|
|
"""
|
|
if not email_config_str:
|
|
return None
|
|
|
|
# Parse the URL
|
|
# Format: smtp://user@domain:password@host:port
|
|
# We need to handle the @ symbols carefully
|
|
match = re.match(r"smtp://([^:]+):([^@]+)@([^:]+):(\d+)", email_config_str)
|
|
if not match:
|
|
raise ValueError(f"Invalid EMAIL_CONFIG format: {email_config_str}")
|
|
|
|
user_with_domain = match.group(1)
|
|
password = unquote(match.group(2))
|
|
host = match.group(3)
|
|
port = int(match.group(4))
|
|
|
|
# Extract email address (user@domain)
|
|
from_email = user_with_domain
|
|
|
|
return {
|
|
"user": user_with_domain,
|
|
"password": password,
|
|
"host": host,
|
|
"port": port,
|
|
"from_email": from_email,
|
|
}
|
|
|
|
|
|
def get_email_config():
|
|
"""Get email configuration from environment."""
|
|
email_config_str = os.environ.get("EMAIL_CONFIG")
|
|
if not email_config_str:
|
|
return None
|
|
return parse_email_config(email_config_str)
|