Zabbix Mail Alert Python script

Source: Internet
Author: User
Tags email account python script

previously saved a Zabbix mail script, this time in the use of a number of problems encountered, here to share with you.

#!/usr/bin/env python#-*- coding: utf-8 -*-import smtplibimport sysfrom  Email.mime.text import mimetextfrom email.header import headerdef send_mail (to_ email,subject,message):    sender =  ' [email protected] '      receiver = to_email    subject =  ' Python email test '     smtpserver =  ' smtp.163.com '     username =  ' XXXXX '     password =  ' xxxxx '     msg = mimetext ( message,  ' plain ',  ' utf-8 ')     msg[' Subject '] = subject     msg[' from '] = sender    msg[' to '] = receiver     smtp = smtplib. SMTP ()     smtp.connect (smtpserver)     smtp.login (username, p assword)     smtp.sendmail (sender, to_email, msg.as_string ())      smtp.quit () if __name__ ==  ' __main__ ':     send_mail (sys.argv[1], SYS.ARGV[2],SYS.ARGV[3])

When the program runs to Smtp.connect (SmtpServer), it is stuck, and using Pycharm on the local Windows PC is no problem

At that time on the Internet to find a variety of information, what add port, ping Smtp,dig SMTP, are not so, and finally I found a script, using other methods, not using Smtp.connect


The script is as follows: The script was found in the rookie tutorial

#!/usr/bin/python# -*- coding: utf-8 -*- import smtplibfrom email.mime.text  import mimetextfrom email.utils import formataddr my_sender= ' [email  Protected] '     #  sender email account my_pass =  ' xxxxxxxxxx '                #  Sender Email Password my_user= ' [email protected] '       #  recipient's email account, my side send to myself Def mail ():     ret=true     try:        msg=mimetext (' Fill in the contents of the message ', ' plain ', ' Utf-8 ')         msg[' from ']=formataddr (["Fromrunoob", My_sender])    #  the corresponding sender's mailbox nickname in parentheses, the sender's email account         msg[' to ']=formataddr (["FK", My_user])               #  Corresponding recipient mailbox nickname in parentheses, Recipient's email account         msg[' Subject ']= "rookie tutorial send mail Test"                  #  the subject of the mail, it can also be said that the title           server=smtplib. Smtp_ssl ("smtp.qq.com",  465)   #  SMTP server in the sender's mailbox, port is 25         server.login (my_sender, my_pass)   #  brackets corresponding to the sender's mailbox account, email password          server.sendmail (My_sender,[my_user,],msg.as_string ())   #  The brackets correspond to the sender's email account, recipient's email account, email         server.quit ()   #  Close connection     except Exception:  #  If the statements in  try  do not execute, the following   Ret=false        ret=false    return ret  ret=mail () if ret:    print ("message sent successfully") Else:    print ("message sent Failed" )

The inside call is Server=smtplib. Smtp_ssl after I test, as long as the network fluctuation is not very large, basic mail can be sent successfully


#!/usr/bin/env python#_*_ coding:utf-8 _*_import smtplib, sysfrom  Email.mime.text import mimetextfrom email.utils import formataddrdef send_mail ( To_email, subject, message):     #邮箱地址和邮箱密码     my_sender  =  ' [email protected] '     my_pass =  ' xxxxx '      my_user = to_email     #发送邮件的信息主体, sender, recipient, content     msg  = mimetext (message,  ' plain ',  ' utf-8 ')     msg[' from '] =  FORMATADDR (["Zabbix",  my_sender])     msg[' to '] = formataddr (["Ops",  my _user])     msg[' Subject '] = subject     #发送邮件      server = smtplib. Smtp_ssl ("smtp.gmail.com",  465)     server.login (my_sender, my_pass)     &nBsp;server.sendmail (My_sender, [my_user, ], msg.as_string ())     server.quit () if __name__ ==  ' __main__ ':     send_mail (sys.argv[1],sys.argv[2],sys.argv[ 3])


Zabbix Mail Alert Python script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.