From b047160dfbaef4ab128355f20dff7631551f8538 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 27 Feb 2026 14:39:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=20SMTP=5FUSER=20=E9=82=AE?= =?UTF-8?q?=E7=AE=B1=E5=90=8E=E7=BC=80=E8=87=AA=E5=8A=A8=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=20SMTP=20=E6=9C=8D=E5=8A=A1=E5=95=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 内置 gmail/qq/163/126/outlook/hotmail/live, 优先级:report-config.json > 自动识别 > 环境变量 Co-Authored-By: Claude Sonnet 4.6 --- skills/weekly-report/send_email.py | 40 ++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/skills/weekly-report/send_email.py b/skills/weekly-report/send_email.py index f5c4960..ea26591 100644 --- a/skills/weekly-report/send_email.py +++ b/skills/weekly-report/send_email.py @@ -2,14 +2,12 @@ """ 周报邮件发送脚本 -SMTP 服务器地址和端口从 report-config.json 读取(不敏感,可提交)。 -账号和密码从环境变量读取(敏感,不入库)。 +SMTP 服务器地址和端口优先级: + 1. report-config.json 中的 smtp_host / smtp_port(最高优先级) + 2. 根据 SMTP_USER 邮箱后缀自动匹配内置服务商 + 3. 环境变量 SMTP_HOST / SMTP_PORT(兜底) -report-config.json 配置项: - smtp_host - SMTP 服务器地址(如 smtphz.qiye.163.com) - smtp_port - SMTP 端口(SSL用465/994,STARTTLS用587) - -必须设置的环境变量: +账号和密码从环境变量读取(敏感,不入库): SMTP_USER - 发件人邮箱 SMTP_PASSWORD - SMTP 密码或授权码 """ @@ -22,6 +20,17 @@ import sys from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +# 内置服务商:邮箱后缀 -> (host, port) +SMTP_PROVIDERS = { + "gmail.com": ("smtp.gmail.com", 587), + "qq.com": ("smtp.qq.com", 465), + "163.com": ("smtp.163.com", 465), + "126.com": ("smtp.126.com", 465), + "outlook.com": ("smtp.office365.com", 587), + "hotmail.com": ("smtp.office365.com", 587), + "live.com": ("smtp.office365.com", 587), +} + def load_config(config_path: str) -> dict: try: @@ -38,13 +47,24 @@ def load_config(config_path: str) -> dict: def get_smtp_config(config: dict) -> tuple: - host = config.get("smtp_host") or os.environ.get("SMTP_HOST") - port = config.get("smtp_port") or os.environ.get("SMTP_PORT", "465") user = os.environ.get("SMTP_USER") password = os.environ.get("SMTP_PASSWORD") + # 根据邮箱后缀自动匹配服务商默认值 + auto_host, auto_port = None, None + if user and "@" in user: + suffix = user.split("@", 1)[1].lower() + if suffix in SMTP_PROVIDERS: + auto_host, auto_port = SMTP_PROVIDERS[suffix] + print(f"自动识别服务商:{suffix} -> {auto_host}:{auto_port}", file=sys.stderr) + + # 优先级:config > 自动识别 > 环境变量 + host = config.get("smtp_host") or auto_host or os.environ.get("SMTP_HOST") + port = config.get("smtp_port") or auto_port or os.environ.get("SMTP_PORT", "465") + if not host: - print("错误:SMTP 服务器地址未配置,请在 report-config.json 中添加 smtp_host", file=sys.stderr) + print("错误:无法确定 SMTP 服务器地址", file=sys.stderr) + print("请在 report-config.json 中添加 smtp_host,或使用内置服务商(gmail/qq/163/outlook)", file=sys.stderr) sys.exit(1) missing = [k for k, v in {"SMTP_USER": user, "SMTP_PASSWORD": password}.items() if not v]