1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
import smtplib import sys import time from email.MIMEText import MIMEText from email.Header import Header from email.utils import parseaddr, formataddr from email import encoders reload(sys) sys.setdefaultencoding('utf8') current_time=time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) mail_host ='smtp.exmail.qq.com' mail_user ='Strong It@*****.com' mail_pwd = '*****'
def _format_addr(s): name, addr = parseaddr(s) return formataddr(( \ Header(name, 'utf-8').encode(), \ addr.encode('utf-8') if isinstance(addr, unicode) else addr))
def send_email( content,mailto, get_sub ): msg = MIMEText( content,_subtype = 'plain', _charset = 'utf-8') msg['From'] = _format_addr(u'Zabbix监控 <%s>' % mail_user) msg['Subject'] = _format_addr(u'报警信息 <%s>' % get_sub) msg['To'] = ",".join( mailto ) try: s = smtplib.SMTP_SSL( mail_host, 465) s.login(mail_user, mail_pwd ) s.sendmail(mail_user, mailto,msg.as_string()) s.close() except Exception as e: return 'Exception: ', e
title = sys.argv[2] cont = """ --------------------------------- 摘要: %s --------------------------------- 时间: %s --------------------------------- """%(sys.argv[3],current_time) to_list = [ '%s'%(sys.argv[1]), ]
with open('/tmp/sendmail_qs.log','ab') as f: f.write('%s Receive address: %s Title: %s \n'%(current_time,sys.argv[1],title))
if __name__ == '__main__': send_email( cont, to_list, title)
|