#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
import sys
from email.mime.text import MIMEText
import time
#reload(sys)
#sys.setdefaultencoding('utf8')
current_time=time.strftime('%Y-%m-%d%H:%M',time.localtime(time.time()))
mail_host='mail.abc.cn'
mail_user='1234@abc.cn'
mail_pwd='*****'
def send_email(mailto,get_sub,content):
#msg = MIMEText( content.encode('utf8'),_subtype = 'html', _charset = 'utf8')
msg = MIMEText(content,_subtype='plain',_charset='gb2312')
msg['From']=mail_user
msg['Subject']=get_sub
#msg['To']=",".join(mailto)
msg['To']=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:
print '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 Send to address: %s Title: %s Substring: %s \n'%(current_time,sys.argv[1],title,sys.argv[3]))
if __name__ == "__main__":
send_email(sys.argv[1], sys.argv[2], sys.argv[3])
测试运行:
[root@node10 alertscripts]# ls
sendmail.py
[root@node10 alertscripts]# pwd
/usr/local/zabbix/share/zabbix/alertscripts
[root@node10 alertscripts]# ./sendmail.py rscpass@163.com rscpass hostalert