使用Flask-Mail发送含表格与文本的渲染模板报告求助
用Flask-Mail发送渲染后的HTML模板邮件
嘿,看起来你已经搞定了Flask-Mail发送文本邮件的基础,现在要升级成带表格和用户输入内容的HTML报告邮件对吧?这其实不难,核心就是用Flask的render_template把你的模板渲染成HTML字符串,然后传给Flask-Mail的Message对象就行。我给你一步步拆解:
1. 确认Flask-Mail基础配置(已配置可跳过)
既然你能发文本邮件,这部分应该没问题,但还是提一下确保万无一失:
from flask import Flask from flask_mail import Mail, Message app = Flask(__name__) # Flask-Mail核心配置 app.config['MAIL_SERVER'] = '你的邮件服务器地址' app.config['MAIL_PORT'] = 587 app.config['MAIL_USE_TLS'] = True app.config['MAIL_USERNAME'] = '你的发件邮箱' app.config['MAIL_PASSWORD'] = '邮箱密码/应用授权码' app.config['MAIL_DEFAULT_SENDER'] = '你的发件邮箱' mail = Mail(app)
2. 创建HTML邮件模板
在Flask项目的templates文件夹下新建report_email.html,把你需要的表格、用户输入区域用Jinja2语法嵌入:
<!DOCTYPE html> <html> <head> <title>ServiceNow事件报告</title> <style> table { border-collapse: collapse; width: 100%; margin: 20px 0; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f5f5f5; } .user-content { padding: 15px; border: 1px solid #eee; border-radius: 4px; } </style> </head> <body> <h2>ServiceNow事件汇总报告</h2> <div class="user-content"> <h3>用户提交备注</h3> <p>{{ user_input }}</p> </div> <h3>事件详情表格</h3> <table> <tr> <th>事件ID</th> <th>事件标题</th> <th>状态</th> <th>优先级</th> </tr> {% for incident in incident_list %} <tr> <td>{{ incident.number }}</td> <td>{{ incident.short_description }}</td> <td>{{ incident.state }}</td> <td>{{ incident.priority }}</td> </tr> {% endfor %} </table> <p>报告生成时间:{{ generate_time }}</p> </body> </html>
这里用{{ user_input }}接收用户填写的文本,用{% for %}循环渲染从ServiceNow拿到的事件数据,还加了基础CSS让邮件排版更美观。
3. 整合数据、渲染模板并发送邮件
结合你提供的incident()函数,写一个处理用户提交、获取数据、发送邮件的视图:
from flask import render_template, request from datetime import datetime # 你原有的ServiceNow数据获取函数 def incident(): service_now_url = SERVICE_NOW_URL request_str = "你的请求字符串/接口逻辑" # 假设这里已经实现数据获取,返回事件列表 incident_list = [ {"number": "INC0010001", "short_description": "系统登录失败", "state": "已解决", "priority": "高"}, {"number": "INC0010002", "short_description": "邮件延迟", "state": "处理中", "priority": "中"} ] return incident_list # 处理用户提交的视图 @app.route('/send-report', methods=['POST']) def send_report(): # 获取用户填写的文本内容 user_input = request.form.get('user_text') if not user_input: return "请填写报告备注内容!", 400 # 从ServiceNow拉取事件数据 incident_list = incident() # 渲染HTML模板 html_content = render_template( 'report_email.html', user_input=user_input, incident_list=incident_list, generate_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S") ) # 创建邮件消息 msg = Message( subject="ServiceNow事件报告", recipients=['收件人邮箱@example.com'], html=html_content # 传入渲染好的HTML内容 ) # 发送邮件 try: mail.send(msg) return "报告已成功发送!" except Exception as e: return f"发送失败:{str(e)}", 500
核心是用render_template把模板转换成HTML字符串,然后传给Message的html参数(body参数对应纯文本内容)。
4. 前端表单配合(可选)
如果是前端表单提交,表单结构大概是这样:
<form action="/send-report" method="POST"> <textarea name="user_text" rows="6" placeholder="请填写报告备注内容..."></textarea> <button type="submit">提交并发送报告</button> </form>
这样用户填写文本点击提交后,后端就会自动拉取ServiceNow数据、渲染模板,发送包含表格和用户输入的HTML邮件啦!
内容的提问来源于stack exchange,提问作者Muks
相关产品推荐
相关产品推荐

