python 实现邮件报警

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
import MySQLdb                                                                                                                                               

def send_mail(to_list,sub,content):
    #设置服务器,用户名、口令以及邮箱的后缀
    mail_host="smtp.163.com"
    mail_user="[email protected]"
    mail_pass=""
    mail_postfix="163.com"
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content)
    msg['Subject'] = sub
    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__':
    strcontent = "这里是报警内容,根据实际监控的情况加"
    mailto_list = ['[email protected]', '[email protected]']
    for mailto in mailto_list:
        send_mail(mailto, "xxx monitor and report", strcontent.encode('utf-8'))

你可能感兴趣的:(python 实现邮件报警)