利用邮件发送附件来实现一键巡检,附件是通过调用zabbix api生成的word和Excel

HTML部分:

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>自动巡检</title>
    <!-- 加入CSS样式 -->
</head>
<body>
    <form id="inspectionForm">
        <label for="email">邮箱地址:</label>
        <input type="email" id="email" name="email" required>

        <label for="subject">主题:</label>
        <input type="text" id="subject" name="subject" required>

        <label for="content">内容:</label>
        <textarea id="content" name="content" required></textarea>

        <button type="submit">一键巡检</button>
    </form>

    <div id="progressBar" style="display:none;">
        <!-- 进度条动画 -->
    </div>

    <script src="/static/script.js"></script>
</body>
</html>

JavaScript部分 (script.js):

javascript 复制代码
document.getElementById("inspectionForm").onsubmit = function(event) {
    event.preventDefault();
    var email = document.getElementById("email").value;
    var subject = document.getElementById("subject").value;
    var content = document.getElementById("content").value;

    // 显示进度条
    document.getElementById("progressBar").style.display = "block";

    // 发送AJAX请求到后端
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/start_inspection", true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            alert("巡检已完成,邮件已发送");
            document.getElementById("progressBar").style.display = "none";
        }
    };
    xhr.send(JSON.stringify({email: email, subject: subject, content: content}));
};

后端(Python + Flask)

后端部分:

python 复制代码
from flask import Flask, request, jsonify, send_file
import subprocess

app = Flask(__name__)

@app.route('/')
def index():
    return send_file('bbb.html')

@app.route('/start_inspection', methods=['POST'])
def start_inspection():
    data = request.get_json()
    email = data['email']
    subject = data['subject']
    content = data['content']

    # 调用Python脚本
    subprocess.run(["python", "C:/Users/YK/Desktop/一键巡检/单独发送邮件包含固定附件已经成功.py", email, subject, content])

    return jsonify({"message": "Inspection completed and email sent"})

if __name__ == '__main__':
    app.run(debug=True)

单独发送邮件包含固定附件已经成功.py

脚本部分:

c 复制代码
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.header import Header
import sys
import os

def send_email(recipient, subject, body, attachment_paths):
    sender = "xunjianbot@163.com"
    password = "XXXXXXXXXXXXX"

    # 创建带有多个部分的邮件
    message = MIMEMultipart()
    message['From'] = Header(sender)
    message['To'] = Header(recipient)
    message['Subject'] = Header(subject)

    # 邮件正文
    message.attach(MIMEText(body, 'plain', 'utf-8'))

    # 添加附件
    for attachment_path in attachment_paths:
        part = MIMEBase('application', "octet-stream")
        try:
            with open(attachment_path, "rb") as file:
                part.set_payload(file.read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachment_path))
            message.attach(part)
        except IOError:
            print(f"Could not attach file: {attachment_path}")
            return

    try:
        server = smtplib.SMTP_SSL("smtp.163.com", 465)
        server.login(sender, password)
        server.sendmail(sender, [recipient], message.as_string())
        server.quit()
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print("邮件发送失败", e)

if __name__ == "__main__":
    if len(sys.argv) != 4:
        print("Usage: python send_email.py <recipient> <subject> <body>")
        sys.exit(1)

    recipient = sys.argv[1]
    subject = sys.argv[2]
    body = sys.argv[3]

    # 附件文件路径列表
    attachment_paths = ["C:\\Users\\YK\\Desktop\\一键巡检\\aaaa.html",
                        "C:\\Users\\YK\\Desktop\\一键巡检\\获取zabbix版本.py"]

    send_email(recipient, subject, body, attachment_paths)
相关推荐
2601_967980118 小时前
Python自动化办公:模块、库与样式调整教程
python·excel·自动化办公·openpyxl·样式调整
用户03321266636711 小时前
使用 Python 轻松加密或解密 Excel 文档
python·excel
总裁余(余登武)2 天前
【python项目】:1操作EXCEl自动化开发
python·自动化·excel
ailitsoft4 天前
库存管理系统如何记录跨门店调拨后的收货差异?
excel·零售·软件需求
IHaveADiscovery4 天前
无痛让AI由Markdown转Word:Pandoc模板了解一下
人工智能·word·markdown
百事牛科技5 天前
Excel的三种密码:防看、防存、防乱改,各管各的
windows·excel
用户298698530145 天前
React 前端将 Excel 工作表导出为图片的技术实践
javascript·react.js·excel
用户0332126663676 天前
使用 Python 在 Excel 文档中添加水印
python·excel
yivifu7 天前
使用lxml操作 Word 的 OpenXML 底层
word·openxml·lxml
科技新资讯8 天前
AI如何批量处理Excel文件? 从数据合并、清洗到自动生成报表,看看 AI Agent 怎么完成任务
大数据·人工智能·excel