将 smtp_host/smtp_port 移入 report-config.json,修复 SSL 端口判断
- report-config.json 新增 smtp_host、smtp_port 字段(不敏感,可提交)
- 环境变量仅保留 SMTP_USER 和 SMTP_PASSWORD
- 修复 port 994 被误判为 STARTTLS 的问题,SSL 端口统一为 {465, 994}
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
c734958fa6
commit
5895c14240
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@ -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__":
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user