diff --git a/config/default.json b/config/default.json index 09d0cf4..621bc6d 100644 --- a/config/default.json +++ b/config/default.json @@ -1,5 +1,6 @@ { "outgoing server": "smtp.gmail.com", + "smtp_port": 587, "email user": "jz7606@gmail.com", "email password": "nxcd ugnt dujo hemx" } diff --git a/send_email_module.py b/send_email_module.py index 3037dcf..1962ffc 100644 --- a/send_email_module.py +++ b/send_email_module.py @@ -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() 的 465→587 自动探测 + """ + 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