python编写邮件发送告警

 

#!/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.163.com’  #这个地方填写邮箱,比如smtp.163.com, smtp.aliyun.com 

    sslPort= 465   #填写端口 

    try:

        msg = MIMEText(unicode(content).encode(‘utf-8’))

        msg[‘from’] = mailfrom

        msg[‘to’] = mailto

        msg[‘Reply-To’] = mailfrom

        msg[‘Subject’] = subject

        smtp = smtplib.SMTP_SSL(gserver,sslPort)

        smtp.set_debuglevel(0)

        smtp.ehlo()

        smtp.login(username,password)

        smtp.sendmail(mailfrom, mailto, msg.as_string())

        smtp.close()

        #smtp = smtplib.SMTP_SSL(smtpHost,sslPort)   

        #smtp.ehlo()   

        #smtp.login(username,password)  

    except Exception,err:

        print “Send mail failed. Error: %s” % err

def main():

    to=sys.argv[1]

    subject=sys.argv[2]

    content=sys.argv[3]

    #下面是填写邮箱账号密码等,邮箱账户两个是一样的 

    sendqqmail(‘填写邮箱’,’密码’,’邮箱’,to,subject,content)

if __name__ == “__main__”:

    main()

 

发表评论