"Python sends Zabbix alarm mail, SSL version" mailman.py

Source: Internet
Author: User

"Python sends Zabbix alarm mail, SSL version" mailman.py

#!/usr/local/bin/python3## via <nosmo king> @ 20141203# ssl only "Usage :     mailman.py  "to"   "subject"   "Body"   "Attachments" description:[-]  1 Recipients:        ./mailman.py  ' [email protected] '   ' test  subject " " simple test content "[-]  multiple recipients:         ./mailman.py  "[email protected],[email protected]"   "Test again"   "another  simple test "[-]  with accessories:        ./mailman.py " [Email  protected] '   ' test1 '   ' test att '   '/tmp/a.log '   '/tmp/1.log '--' from  email.mime.text import mimetextfrom email.mime.base import mimebasefrom  email.mime.multipart import mimemultipartfrom email import encodersimport  Smtplib, os, sys, logging, base64#  sender email_from_1 = {' smtp ': ' smtp.126.com ',  ' account ': ' [email protected] ' ,  ' password ': ' xxx ',  ' nickname ': ' Zbx_svr ',  ' greeting ': ' Dear sir '}#  sender, alternate email_from_2  = {' smtp ': ' smtp.126.com ',  ' account ': ' [email protected] ',  ' password ': ' xxx ',  ' Nickname ': ' Zbx_svr_bak ',  ' greeting ': ' Dear sir '}# +---- logging ---+logging_file  =  '/tmp/mailman.py.log ' Logging.basicconfig (    level = logging. debug,    format =  '% (asctime) s [% (levelname) s]: % (message) s ',     filename = logging_file,    filemode =  ' A ',     ) def delivering (s_from, s_to):     "    s _from:  (smtp, account, password, nickname, greeting)     s_to:   (To, subject, body, attachments)      '         #+----  logging ---+    print ("logging to",  logging_file)      Logging.info ("Now delivering". +------------------------------+from:    {0} <{1}>to:       {3}subject: {4}content:>>{2},{5}>>attachments:{6}+------------------------------+ ". Format (s_from[' nickname '], s_from[' account '], s_from[' greeting '],             s_to[' to '], s_to[' subject '], s_to[' body '], s_to[' Attachments ']))      # email header    m =  Mimemultipart ()     m[' from '] =  ' {0} <{1}> '. Format (s_from[' nickname '),  s_from[' account '])     m[' to '] =  ', '. Join (s_to[' to ') ')     m[' Subject '] = s_to[' Subject ']        #  Email body    content = mimetext (' {0},{1}     '). Format (s_from[' greeting '], s_to[' body '),  ' plain ',  ' utf-8 ')     m.attach ( Content)     # email attachments    for filename in  s_to[' Attachments ']:        with open (filename,  ' RB ')  as f:            addon =  Mimebase (' Application ',  ' Octet-stream ')              addon.set_payload (F.read ())              Encoders.encode_base64 (addon)             addon.add _header (' Content-disposition ',  ' attachment;                      filename= "{0}" '. Format (os.path.basename (filename))              m.attach (addon)     # send  Email    svr = smtplib. SMTP ()     try:        svr.connect (s_from[' SMTP ')         svr.ehlo ()          Svr.starttls ()         svr.ehlo ()           #svr. Set_debuglevel (1)         svr.login (s_from[' account '),  s_from[' password ')         svr.sendmail (s_from[' account '],  s_to[' to '], m.as_string ())         retval = 0    except keyboardinterrupt:         print (' [*] operation aborted! ')         retval = -1    except  Exception as err:        print (' [*] delivering err :  {0} '. Format (err),  file=sys.stderr)         #+----  logging ---+        logging.warning (' delivering: {0} '. Format (err))         retval = -2    finally:         svr.quit ()         #+----  logging ---+    logging.info ("mailman code: {0}". Format (retval))         return retvaldef usage (): &Nbsp;   print (__doc__)     sys.exit (2) if __name__ ==  ' __main_ _ ':     if len (SYS.ARGV)  < 4:         usage ()     email_to = {}    email_to[' to '] =  sys.argv[1].split (', ')     email_to[' subject '] = sys.argv[2]     email_to[' body '] = sys.argv[3]    email_to[' attachments '] =  sys.argv[4:]    try:        retval =  delivering (email_from_1, email_to)         if retval  < 0:            tips =  " Try again, using backup account to deliver. "            &Nbsp;print (Tips)             #+---- logging  ---+            logging.info (tips)              retval = delivering (Email_from_2,  email_to)         msg =  ' mail delivering:  '         msg +=  ' failed! '  if retval else  ' successful! '         print (msg)         #+-- -- logging ---+        logging.info (msg)      except exception as err:        print (' [*]  main err: {0} '. Format (err),  file=sys.stderr)         #  logginG        logging.warning (' {0} '. Format (ERR)) 


"Python sends Zabbix alarm mail, SSL version" mailman.py

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.