Commit 26a5d4e4 authored by liguangyu06's avatar liguangyu06
Browse files

Send CI report mail with SMTP LOGIN instead of email-ext OAuth2.

parent b63cb018
...@@ -23,20 +23,33 @@ def sendReportEmail() { ...@@ -23,20 +23,33 @@ def sendReportEmail() {
if (cc) { if (cc) {
to = [to, cc].findAll { it }.join(',') to = [to, cc].findAll { it }.join(',')
} }
if (!fileExists('reports/latest/email-summary.html')) {
writeFile encoding: 'UTF-8', file: 'reports/latest/email-summary.html', text: body
}
if (!fileExists('reports/latest/email-subject.txt')) {
writeFile encoding: 'UTF-8', file: 'reports/latest/email-subject.txt', text: subject + '\n'
}
try { try {
emailext( def credId = env.SMTP_CREDENTIALS_ID ?: 'sinopharm-smtp'
from: 'liguangyu@sinopharm.com', withCredentials([
replyTo: 'liguangyu@sinopharm.com', usernamePassword(credentialsId: credId, usernameVariable: 'SMTP_USER', passwordVariable: 'SMTP_PASS')
to: to, ]) {
subject: subject, withEnv([
body: body, "EMAIL_TO=${to}",
mimeType: 'text/html', 'SMTP_HOST=mail.sinopharm.com',
attachLog: false, 'SMTP_PORT=465',
attachmentsPattern: 'reports/latest/email-summary.html,reports/latest/email-summary.txt' 'SMTP_FROM=liguangyu@sinopharm.com'
) ]) {
echo "已发送报告邮件至 ${to}(正文为摘要+报告链接,附件为 email-summary.html)" if (isUnix()) {
sh 'python3 scripts/send-ci-email.py'
} else {
bat 'python scripts\\send-ci-email.py'
}
}
}
echo "已发送报告邮件至 ${to}"
} catch (err) { } catch (err) {
echo "邮件发送失败。请安装 Email Extension 插件,并在 Manage Jenkins → System 配置 SMTP:${err}" echo "邮件发送失败(国药邮箱 SMTP LOGIN/465):${err}"
} }
} }
......
#!/usr/bin/env python3
"""Send the CI HTML summary through SMTP LOGIN/SSL. Used by Jenkins post."""
from __future__ import annotations
import os
import smtplib
import ssl
import sys
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
LATEST = ROOT / "reports" / "latest"
def env(name: str, default: str = "") -> str:
return os.environ.get(name, default).strip()
def main() -> int:
to = env("EMAIL_TO")
if not to:
print("[send-ci-email] EMAIL_TO 为空,跳过", file=sys.stderr)
return 2
user = env("SMTP_USER")
password = env("SMTP_PASS")
if not user or not password:
print("[send-ci-email] 未配置 SMTP_USER/SMTP_PASS", file=sys.stderr)
return 2
host = env("SMTP_HOST", "mail.sinopharm.com")
port = int(env("SMTP_PORT", "465") or "465")
sender = env("SMTP_FROM", user)
subject_file = LATEST / "email-subject.txt"
body_file = LATEST / "email-summary.html"
subject = env("EMAIL_SUBJECT")
if not subject and subject_file.exists():
subject = subject_file.read_text(encoding="utf-8").strip()
if not subject:
subject = "SPD UI 自动化测试报告"
if body_file.exists():
body = body_file.read_text(encoding="utf-8")
else:
body = env("EMAIL_BODY") or "<p>未生成摘要,请打开 Jenkins 查看 Playwright 报告。</p>"
msg = MIMEMultipart("alternative")
msg["From"] = sender
msg["To"] = to
msg["Reply-To"] = sender
msg["Subject"] = Header(subject, "utf-8")
msg.attach(MIMEText(body, "html", "utf-8"))
context = ssl.create_default_context()
with smtplib.SMTP_SSL(host, port, context=context, timeout=30) as smtp:
smtp.login(user, password)
smtp.sendmail(sender, [addr.strip() for addr in to.split(",") if addr.strip()], msg.as_string())
print(f"[send-ci-email] SENT_OK to {to}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment