From 9202e4c2028d6cf98f795b5bf476a2af1978e8f2 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Thu, 23 Apr 2026 23:04:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=82=AE=E4=BB=B6=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E8=A7=84=E5=88=99=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 294 ++++++++++++++++++++++++++++++++++++++++++++++++++- logs/app.log | Bin 4441 -> 411755 bytes 2 files changed, 293 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 01319cc..8141e37 100644 --- a/app.py +++ b/app.py @@ -851,6 +851,69 @@ def get_next_run_time(expression): return '无法计算' +# ==================== 邮件通知 API ==================== + +@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: + import smtplib + from email.mime.text import MIMEText + from email.mime.multipart import MIMEMultipart + from datetime import datetime + + # 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', 'favor@tphai.com') + smtp_pass = os.environ.get('SMTP_PASS', 'favor@!') + + server = smtplib.SMTP(smtp_host, smtp_port) + server.ehlo() + server.login(smtp_user, smtp_pass) + + msg = MIMEMultipart() + msg['From'] = smtp_user + msg['To'] = to_email + msg['Subject'] = subject + msg['Date'] = datetime.now().strftime('%a, %d %b %Y %H:%M:%S +0800') + msg['Reply-To'] = to_email + msg.attach(MIMEText(body, 'plain', 'utf-8')) + + server.sendmail(smtp_user, to_email, msg.as_string()) + server.quit() + + return jsonify({'success': True, 'message': '邮件已发送'}) + except Exception as e: + return jsonify({'error': str(e)}), 500 + + +@app.route('/api/email/config', methods=['GET', 'POST']) +def api_email_config(): + """获取/设置邮件配置""" + if request.method == 'GET': + return jsonify({ + 'host': os.environ.get('SMTP_HOST', 'mail.tphai.com'), + 'port': int(os.environ.get('SMTP_PORT', 587)), + 'user': os.environ.get('SMTP_USER', 'favor@tphai.com') + }) + else: + data = request.get_json() + # 只保存到环境变量说明,实际使用需要配置环境变量 + return jsonify({'message': '邮件配置需要通过环境变量设置'}) + + @app.route('/api/crons') def api_crons(): """获取系统 cron 列表(兼容旧接口)""" @@ -1040,7 +1103,7 @@ HTML_TEMPLATE = '''
-