python-e-Mail script

Source: Internet
Author: User

Toss nagios send mail for several days, finally finished, my system environment is ubuntu12.04, install Postfix service, first paste the script, as follows:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
"From: Change to your email address"
Import Datetime,smtplib,string,sys
From email. Header Import Header
From email. Mimetext Import Mimetext
From email. Mimemultipart Import Mimemultipart

def send_mail (TO_ADDR):

If sys.argv[1] = = "Host":
Text=string.join ((
"******** host Problem ********\n\n",
"Notification type:%s\n\n"%sys.argv[2],
"Host Name:%s\n\n"%sys.argv[3],
"IP address:%s\n\n"%sys.argv[4],
"Machine Status:%s\n\n"%sys.argv[5],
"Date:%s\n\n"%sys.argv[6],
"Additional information:%s\n\n"%sys.argv[7]), "\ r \ n")
Else
Text=string.join ((
"******** process/Service problem ********\n\n",
"Notification type:%s\n\n"%sys.argv[1],
"Service Name:%s\n\n"%sys.argv[2],
"Host Name:%s\n\n"%sys.argv[3],
"IP address:%s\n\n"%sys.argv[4],
"Machine Status:%s\n\n"%sys.argv[5],
"Date:%s\n\n"%sys.argv[6],
"Additional information:%s\n\n"%sys.argv[7]), "\ r \ n")

msg = Mimemultipart ()
Msg[' to '] = to_addr
Msg[' from '] = ' [email protected] '
msg[' Subject ' = Header (' Detect SSH Port results (' + str (datetime.date.today ()) + ') ', ' utf-8 ')
Text_msg = Mimetext (text, ' Plain ', ' utf-8 ')
Msg.attach (TEXT_MSG)
Server = Smtplib. SMTP (' smtp.sina.com ')
Server.login (' [email protected] ', ' xxxxxx ')
Server.sendmail (msg[' from '],msg[' to '],msg.as_string ())
Server.close ()

if __name__ = = ' __main__ ':
addr_list=[' [email protected] ', ' [email protected] ', ' [email protected] '
For I in range (len (addr_list)):
Send_mail (Addr_list[i])


Note:

1. Use the Mimemultipart method to create a MIME object that can contain attachments;

2, using the Mimetext method to create the content of the message object;

3, use Smtplib. SMTP method to implement the mail system login and send mail;

4. The contents of the message are obtained from the macro variable parameter one by one of Nagios.

This article is from the "Snail's Home" blog, be sure to keep this source http://winters.blog.51cto.com/5617866/1590188

python-e-Mail script

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.