#!/usr/bin/python2.7
#__*__coding: utf-8__*__
import smtplib
from email.mime.text import MIMEText
import os
import argparse
import logging
import datetime
import sys
from email.header import Header
reload(sys)
sys.setdefaultencoding(‘utf-8’)
mail_host = ‘smtp.xxxxxx.com’
mail_user = ‘ip’
mail_server = ‘zabbix’
mail_pass = ‘password’
mail_postfix = ‘xxxxxxxx.com’
logpath=’/var/log/zabbix/alert’
def send_mail(mail_to,subject,content):
me = mail_server+”<” + mail_user+”@”+mail_postfix+”>”
msg = MIMEText(content,’plain’, ‘utf-8’)
msg[‘Subject’] = Header(args.subject,’utf-8′)
msg[‘From’] = me
msg[‘to’] = mail_to
global sendstatus
global senderr
try:
smtp=smtplib.SMTP()
smtp.connect(mail_host)
smtp.login(mail_user,mail_pass)
smtp.sendmail(me,mail_to,msg.as_string())
smtp.close()
print(‘send ok’)
sendstatus = True
except Exception,e:
senderr = str(e)
print senderr
sendstatus = False
def logwrite(sendstatus,mail_to,content,logpath):
if not sendstatus:
content = senderr
if not os.path.isdir(logpath):
os.makedirs(logpath)
t=datetime.datetime.now()
daytime = t.strftime(‘%Y-%m-%d’)
daylogfile= logpath+’/’+str(daytime) + ‘.log’
logging.basicConfig(filename=daylogfile,level=logging.DEBUG)
logging.info(‘*’*130)
logging.debug(str(t)+’mail send to {0},content is: \n {1}’.format(mail_to,content))
if __name__ == ‘__main__’:
parser = argparse.ArgumentParser(description=’Send mail to user for zabbix alerting’)
parser.add_argument(‘mail_to’,action=’store’,help=’The address of the email that send to user ‘)
parser.add_argument(‘subject’,action=’store’,help=’The subject of the email ‘)
parser.add_argument(‘content’,action=’store’,help=’The content of the email’)
args = parser.parse_args()
mail_to=args.mail_to
subject=args.subject
content=args.content
send_mail(mail_to,subject,content)
logwrite(sendstatus,mail_to,content,logpath)