因爲阿里雲只能用465端口。因此這個zabbix的腳本修改爲了465端口的python腳本。html
修改於https://www.jianshu.com/p/9d6941dabb47python
#!/usr/bin/env python # -*- coding: UTF-8 -*- import smtplib import traceback from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import sys def sendmail(subject, msg, toaddrs, fromaddr, smtpaddr, password): ''' @subject:郵件主題 @msg:郵件內容 @toaddrs:收信人的郵箱地址 @fromaddr:發信人的郵箱地址 @smtpaddr:smtp服務地址,能夠在郵箱看,好比163郵箱爲smtp.163.com @password:發信人的郵箱密碼 ''' mail_msg = MIMEMultipart() if not isinstance(subject, unicode): subject = unicode(subject, 'utf-8') mail_msg['Subject'] = subject mail_msg['From'] = fromaddr mail_msg['To'] = ','.join(toaddrs) mail_msg.attach(MIMEText(msg, 'html', 'utf-8')) try: s = smtplib.SMTP_SSL() s.connect(smtpaddr, 465) # 鏈接smtp服務器 s.login(fromaddr, password) # 登陸郵箱 s.sendmail(fromaddr, toaddrs, mail_msg.as_string()) # 發送郵件 s.quit() except Exception, e: print "Error: unable to send email" print traceback.format_exc() if __name__ == '__main__': fromaddr = "yeyu@XXXX.com" smtpaddr = "smtp.XXXX.com" password = "XXXX" toaddrs = [sys.argv[1]] subject = sys.argv[2] msg = sys.argv[3] # toaddrs =sys.argv[1] # subject=sys.argv[2] # msg = sys.argv[3] sendmail(subject, msg, toaddrs, fromaddr, smtpaddr, password)