This commit is contained in:
hailin 2025-07-21 21:22:06 +08:00
parent 7a53c7662c
commit 36fad874ef
2 changed files with 22 additions and 1 deletions

View File

@ -1,5 +1,6 @@
{
"outgoing server": "smtp.gmail.com",
"smtp_port": 587,
"email user": "jz7606@gmail.com",
"email password": "nxcd ugnt dujo hemx"
}

View File

@ -56,6 +56,25 @@ def xor_encode(src: str, key: int) -> str:
"""把邮箱做异或后返回 16 进制字符串"""
return "".join(f"{ord(c) ^ key:02x}" for c in src)
# ---------- 读取 port 并建立连接 ---------- ✨ new
def connect_smtp(host: str, config: dict):
"""
优先按 config["smtp_port"] 建联
465 直接 SSL
其它端口 先连再 starttls()
若没写端口 回退 detect_smtp_port() 465587 自动探测
"""
fixed_port = int(config.get("smtp_port", 0)) # 没写就 0
if fixed_port:
if fixed_port == 465:
return smtplib.SMTP_SSL(host, 465, timeout=20)
s = smtplib.SMTP(host, fixed_port, timeout=20)
s.starttls(context=ssl.create_default_context())
return s
# 没配端口 → 用原自动探测
return detect_smtp_port(host)
# ---------- send_email 主函数 ----------
def send_email(key, to_address, subject, body_template, recipient_name,
@ -103,8 +122,9 @@ def send_email(key, to_address, subject, body_template, recipient_name,
# --- 发送尝试 ---
MAX_RETRIES, RETRY_DELAY = 6, 600 # 最多 1 小时
host = config['outgoing server'].strip() # ✨ new放在循环前也行
for attempt in range(MAX_RETRIES):
server = detect_smtp_port(config['outgoing server'].strip())
server = connect_smtp(host, config)
if server is None:
result.update(status="fail", message="无法连接到任何 SMTP 端口")
return result