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
):
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}));
};
后端部分:
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
脚本部分:
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)