根据 SMTP_USER 邮箱后缀自动匹配 SMTP 服务商
内置 gmail/qq/163/126/outlook/hotmail/live, 优先级:report-config.json > 自动识别 > 环境变量 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
5895c14240
commit
b047160dfb
@ -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_USER - 发件人邮箱
|
||||||
SMTP_PASSWORD - SMTP 密码或授权码
|
SMTP_PASSWORD - SMTP 密码或授权码
|
||||||
"""
|
"""
|
||||||
@ -22,6 +20,17 @@ import sys
|
|||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
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:
|
def load_config(config_path: str) -> dict:
|
||||||
try:
|
try:
|
||||||
@ -38,13 +47,24 @@ def load_config(config_path: str) -> dict:
|
|||||||
|
|
||||||
|
|
||||||
def get_smtp_config(config: dict) -> tuple:
|
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")
|
user = os.environ.get("SMTP_USER")
|
||||||
password = os.environ.get("SMTP_PASSWORD")
|
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:
|
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)
|
sys.exit(1)
|
||||||
|
|
||||||
missing = [k for k, v in {"SMTP_USER": user, "SMTP_PASSWORD": password}.items() if not v]
|
missing = [k for k, v in {"SMTP_USER": user, "SMTP_PASSWORD": password}.items() if not v]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user