zabbix发送邮件python脚本

时间:2021-07-01 08:48:36
#!/usr/bin/python 
# coding:utf-8
# 适用于python 2.x,解决python发送邮件乱码问题
 
import smtplib 
from email.mime.text import MIMEText 
import sys 
 
mail_host = 'smtp.mail.com' 
mail_user = 'yourmail@mail.com' 
mail_pass = 'passwd' 
mail_postfix = 'mail.com' 
 
def send_mail(to_list,subject,content): 
    me = mail_user+"<"+mail_user+"@"+mail_postfix+">" 
    # 增加 _charset="utf-8" ,使MIMEText使用utf-8编码,避免中文乱码
    msg = MIMEText(content,_charset="utf-8")
    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])