標籤:python mail python mail
#!/usr/bin/env python# -*- coding: utf-8 -*-import sysreload(sys)from email.MIMEText import MIMETextimport smtplibsys.setdefaultencoding(‘utf-8‘)import socket, fcntl, structdef send_mail(to_list,sub,content): mail_host="smtp.163.com" mail_user="[email protected]" mail_pass="XXXXXXX" 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 Falsedef get_local_ip(ifname = ‘eth0‘): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack(‘256s‘, ifname[:15])) ret = socket.inet_ntoa(inet[20:24]) return retif sys.argv[1]!="master" and sys.argv[1]!="backup" and sys.argv[1]!="fault": sys.exit()else: notify_type = sys.argv[1]if __name__ == ‘__main__‘: strcontent = get_local_ip()+ " " +notify_type+" State is activated, please make sure the HAProxy service running state!" mailto_list = [‘[email protected], ]for mailto in mailto_list: send_mail(mailto, "HAProxy State switch alarm", strcontent.encode(‘utf-8‘))
參考南非螞蟻《高效能Linux伺服器構建實戰》
本文出自 “李惟忠的技術部落格” 部落格,請務必保留此出處http://liweizhong.blog.51cto.com/1383716/1639917
python 郵件通知程式