利用邮件发送附件来实现一键巡检,附件是通过调用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)
相关推荐
不吃鱼的羊2 小时前
Excel生成DBC脚本源文件
服务器·网络·excel
chenchihwen2 小时前
数据分析时的json to excel 转换的好用小工具
数据分析·json·excel
lxxxxl5 小时前
C#调用OpenXml,读取excel行数据,遇到空单元跳过现象处理
excel
m0_748246355 小时前
前端通过new Blob下载文档流(下载zip或excel)
前端·excel
SEO-狼术14 小时前
Document Solutions for Word CRACK
word
不吃鱼不吃鱼15 小时前
Excel加载项入门:原理、安装卸载流程与常见问题
excel·wps
深耕AI15 小时前
在Excel中绘制ActiveX控件:解决文本编辑框定位问题
java·前端·excel
五VV15 小时前
Note2024122001_Excel按成绩排名
excel
Eiceblue16 小时前
Python拆分Excel - 将工作簿或工作表拆分为多个文件
开发语言·python·excel
Excel_easy16 小时前
批量生成二维码,助力数字化管理-Excel易用宝
excel·wps