- background
- The QQ mailbox alarm fails all the time. After checking a lot of data, you can find the ssl rules and set the port settings
- Script content (163 mailbox, 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'
##Define outgoing message type
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]
##Define the account and password of QQ email, you need to change it to your own account and password (please don't put the real user name and password on the Internet, or you will die miserably)
sendqqmail('163 mailbox','Password','163 mailbox',to,subject,content)
if __name__ == "__main__":
main()
#####Script instructions######
#1. First define the email account and password in the script
#2. The script execution command is: python mail.py target mailbox "mail subject" and "mail content"
//Save exit
- When using this script to alert QQ mailbox, you will find various errors. In fact, if you want to use it, you can change some details slightly
- First position
Change gport = 25 to gport = 465
Because the QQ mailbox needs to use SSL, the port number is 465 or 587
take smtp = smtplib.SMTP(gserver, gport)
smtp = smtplib.SMTP_SSL(gserver, gport)
#SSL connection, change it to smtp = smtplib.SMTP_SSL(gserver, gport)
The final QQ email 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'
##Define outgoing message type
gport = 465
try:
msg = MIMEText(unicode(content).encode('utf-8'))
msg['from'] = mailfrom
msg['to'] = mailto
msg['Reply-To'] = mailfrom
msg['Subject'] = subject
#SSL connection, change the following to 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]
##Define the account and password of QQ email, you need to change it to your own account and password (please don't put the real user name and password on the Internet, or you will die miserably)
sendqqmail('781851883@qq.com','mjjqsasaqxfwbcdj','781851883@qq.com',to,subject,content)
if __name__ == "__main__":
main()
#####Script instructions######
#1. First define the email account and password in the script
#2. The script execution command is: python mail.py target mailbox "mail subject" and "mail content"
[root@hf-01 alertscripts]#