zabbix 4.2 发送警告邮件Python脚本

时间:2020-12-25 08:54:22

#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os,sys
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from subprocess import *
def sendqqmail(username,password,mailfrom,mailto,subject,content):
gserver = 'smtp.xx.com'
gport = 25
try:
msg = MIMEText(content,'plain','utf-8')
msg['from'] = mailfrom
msg['to'] = mailto
msg['Reply-To'] = mailfrom
msg['Subject'] = subject
smtp = smtplib.SMTP(gserver, gport)
smtp.set_debuglevel(0)
smtp.ehlo()
smtp.login(username,password)
smtp.sendmail(mailfrom, mailto, msg.as_string())
smtp.close()
except Exception,err:
print "Send mail failed. Error: %s" % err
def main():
to=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]
sendqqmail('xx@xx.com','password','xx@xx.com',to,subject,content)

if __name__ == "__main__":
main()

 

#注 :在本人测试环境已经成功,因为不是自己写的,所以有问题还请百度自行解决。