此脚本用与发送邮件,使用前需安装python2.7及以上版本
使用方法:
python sendmail "邮件标题" "邮件内容"(假定脚本名为sendmail)
代码如下:
#!/usr/local/python27/bin/python2.7
#-*- coding: utf-8 -*-
#=============================================================================
# FileName:
# Desc:
# Author: Rocky
# Email: zutianbiao@qq.com
# HomePage: http://zutianbiao.blog.51cto.com
# Version: 0.0.1
# LastChange: 2013-02-20 14:52:11
# History:
#=============================================================================
from multiprocessing import Process
import re,sys,urllib,codecs,os
import string
import smtplib
from email.mime.text import MIMEText
#############
mailto_list=["example1@example.com","example2@example.com"]
#####################
#设置服务器,用户名、口令以及邮箱的后缀(需根据具体情况修改)
mail_host="mailserver.example.com"
mail_user="username"
mail_pass="password"
mail_postfix="example.com"
######################
def send_mail(to_list,sub,content):
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
nagios_title = sys.argv[1]
nagios_lr = sys.argv[2]
send_mail(mailto_list,nagios_title,nagios_lr)
转载于:https://blog.51cto.com/zutianbiao/1363904