系统监控,邮箱发送告警信息:
四、自定义的模块:(EmailUtil.py)
import smtplib # 发送邮件 from email.mime.text import MIMEText import json import configparser # 解析配置文件
config = configparser.ConfigParser() #实例化 config.read(filenames='D:/Holz/python运维/maintenance/maintenance/lib/util/config.ini',encoding='utf-8') #读取文件 # 邮件服务器:smtp.qq.com # 邮箱登录账户:[email protected] # 邮箱账户的密码: # 接收方: # 邮件内容: # def get_config(): # print("获取配置文件") # # with open("config.json", "r") as f: # # config = json.load(f) # # print(config) # return sections def send_msg(receiver, content): print("邮件配置内容为:{}".format(config)) print("给{}发送邮件,邮件的内容为:{}".format(receiver, content)) # 构造邮件正文 message = MIMEText(content, "plain", "utf-8") message["From"] = config["email"]["user"] message["To"] = receiver message["Subject"] = "签收信" # 构造邮件发送对象 smtp = smtplib.SMTP() # 连接邮箱服务器 smtp.connect(config["email"]["host"]) # 登录邮箱服务器 smtp.login(config["email"]["user"], config["email"]["password"]) # 发送邮件 smtp.sendmail(config["email"]["user"], receiver, message.as_string()) email = config["email"] receiver = ["[email protected]","[email protected]"] for item in receiver: send_msg(receiver=item, content="这是一份快乐,请签收")
快乐的签收信: