日历存档: 2018 年 5 月 5 日

Zabbix使用Pyhton脚本发送邮件

分类:网络技术日期:2018-05-05 - 11:37:31评论:1条作者:老谢

测试3.2和3.4均可用,通知媒介里面,给三个参数,顺序为:{ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}

#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)
 
mail_host = 'smtp.xxx.cn'
mail_user = 'xxx'
mail_pass = 'xxx'
mail_postfix = 'xxx.cn'
def send_mail(to_list,subject,content):
    me = mail_user+"@"+mail_postfix
    msg = MIMEText(content, 'plain', 'utf-8')
    msg["Accept-Language"]="zh-CN"
    msg["Accept-Charset"]="ISO-8859-1,utf-8"
    if not isinstance(subject,unicode):
        subject = unicode(subject)
    msg['Subject'] = subject
    msg['From'] = me
    msg['to'] = to_list
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user,mail_pass)
        s.sendmail(me,to_list,msg.as_string())
        s.close()
        return True
    except Exception,e:
        print str(e)
        return False
if __name__ == "__main__":
    send_mail(sys.argv[1], sys.argv[2], sys.argv[3])