Files
project-panel/routes_email.py
hz4th_coder 16400b0359 fix: 修复邮件发送通道
- 发件账号改为 hz4th_coder@tphai.com(替换无效的 favor@tphai.com)
- 发件人名称: 黄庄4号程序员
- 连接前添加 server.ehlo() 调用(对齐 send_email.py 工作脚本)
- 无SSL/无STARTTLS 模式
2026-06-01 17:28:31 +08:00

60 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
邮件通知 API 路由
"""
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from flask import jsonify, request
from utils import log_message
def register_email_routes(app):
"""注册邮件通知相关路由"""
@app.route('/api/email/send', methods=['POST'])
def api_email_send():
"""发送邮件通知"""
data = request.get_json()
if not data:
return jsonify({'error': '无效数据'}), 400
to_email = data.get('to')
subject = data.get('subject')
body = data.get('body')
if not to_email or not subject or not body:
return jsonify({'error': '缺少必要参数'}), 400
try:
# SMTP配置 (从环境变量或默认值)
smtp_host = os.environ.get('SMTP_HOST', 'mail.tphai.com')
smtp_port = int(os.environ.get('SMTP_PORT', 587))
smtp_user = os.environ.get('SMTP_USER', 'hz4th_coder@tphai.com')
smtp_pass = os.environ.get('SMTP_PASS', 'hz4th_coder@!')
# 创建邮件
msg = MIMEMultipart()
from email.utils import formataddr
msg['From'] = formataddr(('黄庄4号程序员', smtp_user))
msg['To'] = to_email
msg['Subject'] = subject
# 添加正文
msg.attach(MIMEText(body, 'plain', 'utf-8'))
# 发送邮件无SSL模式需要先ehlo
server = smtplib.SMTP(smtp_host, smtp_port)
server.ehlo()
server.login(smtp_user, smtp_pass)
server.send_message(msg)
server.quit()
log_message(f"邮件发送成功: {subject} -> {to_email}")
return jsonify({'message': '邮件发送成功'})
except Exception as e:
log_message(f"邮件发送失败: {e}")
return jsonify({'error': str(e)}), 500