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 = '''
-