- background
- The use of QQ mailbox alarm has always failed, I checked a lot of information, found ssl rules, and set the port settings.
- Script content (163 mailboxes, can be used directly)
[root@hf-01 ~]# vim /usr/lib/zabbix/alertscripts/mail.py
#!/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'
##定义发邮件类型
gport = 25
try:
msg = MIMEText(unicode(content).encode('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('163邮箱','密码','163邮箱',to,subject,content)
if __name__ == "__main__":
main()
#####脚本使用说明######
#1. 首先定义好脚本中的邮箱账号和密码
#2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"
保存退出
- When using the script QQ mailbox alarm, you will find various errors. In fact, if you want to use it, you can change some details slightly
- first location
将gport = 25改为 gport = 465
因为QQ邮箱需要使用SSL,端口号为465或者587
将smtp = smtplib.SMTP(gserver, gport)
smtp = smtplib.SMTP_SSL(gserver, gport)
#ssl连接,把它改为smtp = smtplib.SMTP_SSL(gserver, gport)
The final QQ mailbox alert script is as follows
[root@hf-01 alertscripts]# cat /usr/lib/zabbix/alertscripts/qqmail.py
#!/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 = 465
try:
msg = MIMEText(unicode(content).encode('utf-8'))
msg['from'] = mailfrom
msg['to'] = mailto
msg['Reply-To'] = mailfrom
msg['Subject'] = subject
#ssl连接,把下面改为smtp = smtplib.SMTP_SSL(gserver, gport)
smtp = smtplib.SMTP_SSL(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]','mjjqsasaqxfwbcdj','[email protected]',to,subject,content)
if __name__ == "__main__":
main()
#####脚本使用说明######
#1. 首先定义好脚本中的邮箱账号和密码
#2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"
[root@hf-01 alertscripts]#