Python sends e-mail to the specified user via the SMTP service (for Zabbix mail alerts)

Source: Internet
Author: User

The present free mail Service is many, for example 163 enterprise mailbox, QQ Enterprise mailbox and so on. Do not need to build a mail server to send mail to the designated user, only need to register any one of the SMTP protocol support mailbox can be implemented to send mail. Messages can be sent via Linux commands, shell scripts written by themselves, or Python scripts written in Python.

The following code is a simple but practical example. By default, when no parameters are executed, a preset message subject and message content is sent to the preset user. Sends the specified subject and message content to the specified user when the parameter is executed. With parameter execution can be used for Zabbix mail alert scripts.

For zabbix2.x you can fill in the script name directly. For zabbix3.x, you need to specify a parameter, the first is parameter 1, the second is parameter 2, and so on.

As shown in the following:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/82/74/wKioL1dWKSaSK9pPAAJVA_4VyT4855.jpg-wh_500x0-wm_3 -wmp_4-s_3238413178.jpg "title=" 2016-06-07_094540.jpg "alt=" Wkiol1dwksask9ppaajva_4vyt4855.jpg-wh_50 "/>

The code is as follows:

#!/usr/bin/python# encoding: utf-8# -*- coding: utf8 -*-import  Smtplibimport stringimport sysdef usage ():     print ("" "     Function: send email to somebody using smtp protocol     usage:        no parameters:   python  %s        with parameters: python %s < Mailto> <subject> <message body>    example: python  %s  "SendTo"   "subject"   "Message" "" " %  (__file__, __file__, sys.argv[0 ])     sys.exit (0) email_host =  "Smtp.example.domain"                # change itemail_port = 25                                   # default smtp portEMAIL_HOST_USER =  ' [ Email protected] '       # change itemail_host_password =   ' Your password '            # change  itDEFAULT_FROM_EMAIL =  ' [email protected] '    # change itcrlf  =  "\ r \ n"                                      # for Windows user read easilyEMAIL_TO =  "[email protected ] "                                                                        # user defined variable, in  zabbix is {alert. sendto}subject =  "An email notification from python"                                                                # user defined variable, in zabbix is {alert. subject}text =  "If you saw this content, it means it works  and this&nBsp;is default content with no parameters. "      # user defined variable, in zabbix is {alert. Message}argc = len (SYS.ARGV) if not  (argc == 1 or argc == 4):     print ("error: incorrect number of arguments or  Unrecognized option ")     usage () if argc == 1:     PASSELSE:&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;SYS.ARGV[1]&NBSP;IS&NBSP;NOT&NBSP;NONE&NBSP;AND&NBSP;SYS.ARGV[2]  is not None and sys.argv[3] is not None:         EMAIL_TO = sys.argv[1]         SUBJECT&NBSP;=&NBSP;SYS.ARGV[2]&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;TEXT&NBSP;=&NBSP;SYS.ARGV[3] Body = string.join (     "from: %s"  % D efault_from_email,     "to: %s"  % EMAIL_TO,     " subject: %s " % SUBJECT,    ",     text), &NBSP;CRLF) Server = smtplib. SMTP () server.connect (Email_host, email_port) Server.starttls () Server.login (Email_host_user, email_ Host_password) Server.sendmail (default_from_email, [email_to], body) server.quit ()

Tag:python Mail Alarm, Zabbix mail alarm script, python send mail

--end--

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1786821

Python sends e-mail to the specified user via the SMTP service (for Zabbix mail alerts)

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.