From 5895c14240fe07e2df7724dbced5240d81a87c07 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 27 Feb 2026 14:33:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20smtp=5Fhost/smtp=5Fport=20=E7=A7=BB?= =?UTF-8?q?=E5=85=A5=20report-config.json=EF=BC=8C=E4=BF=AE=E5=A4=8D=20SSL?= =?UTF-8?q?=20=E7=AB=AF=E5=8F=A3=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - report-config.json 新增 smtp_host、smtp_port 字段(不敏感,可提交) - 环境变量仅保留 SMTP_USER 和 SMTP_PASSWORD - 修复 port 994 被误判为 STARTTLS 的问题,SSL 端口统一为 {465, 994} Co-Authored-By: Claude Sonnet 4.6 --- skills/weekly-report/SKILL.md | 4 +++- skills/weekly-report/send_email.py | 37 +++++++++++++++++------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/skills/weekly-report/SKILL.md b/skills/weekly-report/SKILL.md index 0cba327..49e5aab 100644 --- a/skills/weekly-report/SKILL.md +++ b/skills/weekly-report/SKILL.md @@ -13,7 +13,9 @@ argument-hint: "" ```json { "recipients": ["boss@company.com", "team@company.com"], - "project_name": "项目名称" + "project_name": "项目名称", + "smtp_host": "smtphz.qiye.163.com", + "smtp_port": 994 } ``` diff --git a/skills/weekly-report/send_email.py b/skills/weekly-report/send_email.py index 8f35c62..f5c4960 100644 --- a/skills/weekly-report/send_email.py +++ b/skills/weekly-report/send_email.py @@ -1,11 +1,15 @@ #!/usr/bin/env python3 """ 周报邮件发送脚本 -SMTP 凭证从环境变量读取,不接受命令行参数传入凭证。 + +SMTP 服务器地址和端口从 report-config.json 读取(不敏感,可提交)。 +账号和密码从环境变量读取(敏感,不入库)。 + +report-config.json 配置项: + smtp_host - SMTP 服务器地址(如 smtphz.qiye.163.com) + smtp_port - SMTP 端口(SSL用465/994,STARTTLS用587) 必须设置的环境变量: - SMTP_HOST - SMTP 服务器地址 - SMTP_PORT - SMTP 端口(SSL用465,STARTTLS用587) SMTP_USER - 发件人邮箱 SMTP_PASSWORD - SMTP 密码或授权码 """ @@ -33,18 +37,17 @@ def load_config(config_path: str) -> dict: sys.exit(1) -def get_smtp_config() -> tuple: - host = os.environ.get("SMTP_HOST") - port = os.environ.get("SMTP_PORT", "465") +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") - missing = [k for k, v in { - "SMTP_HOST": host, - "SMTP_USER": user, - "SMTP_PASSWORD": password, - }.items() if not v] + if not host: + print("错误:SMTP 服务器地址未配置,请在 report-config.json 中添加 smtp_host", file=sys.stderr) + sys.exit(1) + missing = [k for k, v in {"SMTP_USER": user, "SMTP_PASSWORD": password}.items() if not v] if missing: print(f"错误:缺少以下环境变量:{', '.join(missing)}", file=sys.stderr) print("请在 shell 中设置(不要写入任何文件):", file=sys.stderr) @@ -55,14 +58,14 @@ def get_smtp_config() -> tuple: try: port = int(port) except ValueError: - print(f"错误:SMTP_PORT 必须是数字,当前值为 '{port}'", file=sys.stderr) + print(f"错误:smtp_port 必须是数字,当前值为 '{port}'", file=sys.stderr) sys.exit(1) return host, port, user, password -def send_email(recipients: list, subject: str, body_html: str, sender: str): - host, port, user, password = get_smtp_config() +def send_email(recipients: list, subject: str, body_html: str, sender: str, config: dict): + host, port, user, password = get_smtp_config(config) msg = MIMEMultipart("alternative") msg["Subject"] = subject @@ -70,8 +73,10 @@ def send_email(recipients: list, subject: str, body_html: str, sender: str): msg["To"] = ", ".join(recipients) msg.attach(MIMEText(body_html, "html", "utf-8")) + # 465 和 994 均为 SSL 直连端口,587 使用 STARTTLS + ssl_ports = {465, 994} try: - if port == 465: + if port in ssl_ports: with smtplib.SMTP_SSL(host, port, timeout=30) as smtp: smtp.login(user, password) smtp.sendmail(user, recipients, msg.as_string()) @@ -114,7 +119,7 @@ def main(): print(f"错误:找不到邮件正文文件 {args.body_file}", file=sys.stderr) sys.exit(1) - send_email(recipients, args.subject, body_html, sender=config.get("sender", "")) + send_email(recipients, args.subject, body_html, sender=config.get("sender", ""), config=config) if __name__ == "__main__":