zabbix傳送郵件指令碼
阿新 • • 發佈:2018-12-01
#!/usr/bin/env python #-*- coding: UTF-8 -*- import os,sys reload(sys) sys.setdefaultencoding('utf8') import getopt import smtplib from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from subprocess import * def sendqqmail(username,password,mailfrom,mailto,subject,content): gserver= 'smtp.qq.com' gport = 25 try: # msg = MIMEText(unicode(content).encode('utf-8')) //如果傳送的郵件有亂碼,可以嘗試把這行改成如下: msg = MIMEText(content,'plan','utf-8') msg['from'] = mailfrom msg['to'] = mailto msg['Reply-To'] = mailfrom msg['Subject'] = subject smtp= smtplib.SMTP(gserver, gport) smtp.set_debuglevel(0) smtp.ehlo() smtp.login(username,password) smtp.sendmail(mailfrom, mailto, msg.as_string()) smtp.close() except Exception,err: print "Send mail failed. Error: %s" % err def main(): to=sys.argv[1] subject=sys.argv[2] content=sys.argv[3] ##定義QQ郵箱的賬號和密碼,你需要修改成你自己的賬號和密碼(請不要把真實的使用者名稱和密碼放到網上公開,否則你會死的很慘) sendqqmail('[email protected]','aaaaaaaaaa','[email protected]',to,subject,content) if __name__ == "__main__": main() #####指令碼使用說明###### #1. 首先定義好指令碼中的郵箱賬號和密碼 #2. 指令碼執行命令為:python mail.py 目標郵箱 "郵件主題" "郵件內容"