Python mail script application

Source: Internet
Author: User

Http://blog.csdn.net/jrckkyy/archive/2010/03/12/5373427.aspx

 

I spent some time learning Python a while ago. Recently, I completed a project to monitor basic server information. It is a distributed system that satisfies everyone's desire to monitor, and generates special logs and alarms, A single server collects about 1440 logs per day once per minute. Currently, more than 20 servers are monitored, and about 31680 logs are collected each day, at present, the single-point monitoring center has more than enough servers in terms of performance. More servers can be used for testing. It is estimated that more than 100 servers can be monitored.

One requirement is to send messages to relevant personnel in real time when an alarm is detected. As the company's SMS gateway only buys Shanghai Telecom users and does not have Shanghai Telecom's number, Khan has to send an email.

Supports sending gb18030 encoded text content, any encoding of attachments, you can make appropriate modifications to support group sending.

# Coding = UTF-8
#! /Usr/lib/python2.5/bin/Python
Import OS
Import sys
From smtplib import SMTP
From email. mimemultipart import mimemultipart
From email. Mime. Application import mimeapplication
From email. mimetext import mimetext
From email. mimebase import mimebase
From email import utils, encoders
Import mimetypes
Import time

Stmp_server = "mail. ×××. com"
Stmp_port = "25"
Username = "XXX @ xxx. com"
Userpassword = "×××"
From = "monitorcenterwarning @ ×××. com"
To = "××× @ gmail.com"

Def sendfildbymail (config ):
Print 'preparing ...'
Message = mimemultipart ()
Message ['from'] = config ['from']
Message ['to'] = config ['to']
Message ['reply-to'] = config ['from']
Message ['subobject'] = config ['subobject']
Message ['date'] = time. ctime (Time. Time ())
Message ['x-priority '] = '3'
Message ['x-msmail-priority '] = 'normal'
Message ['x-mailer '] = 'Microsoft Outlook Express 6.00.2900.2180'
Message ['x-mimeole'] = 'produced by Microsoft mimeole v6.00.2900.2180'

If 'file' in config:
# Add attachment
F = open (config ['file'], 'rb ')
File = mimeapplication (F. Read ())
F. Close ()
File. add_header ('content-disposition', 'attachment', filename = OS. Path. basename (config ['file'])
Message. Attach (file)

If 'content' in config:
# Add text content
F = open (config ['content'], 'rb ')
F. Seek (0)
Content = f. Read ()
Body = mimetext (content, 'base64', 'gb2312 ')
Message. Attach (Body)

Print 'okay'
Print 'logging ...'
SMTP = SMTP (config ['server'], config ['Port'])
# Add comments to the following line if the SMTP server does not need to verify logon when sending an email
SMTP. login (config ['username'], config ['Password'])
Print 'OK'

Print 'sending ...',
SMTP. Sendmail (config ['from'], [config ['from'], config ['to'], message. as_string ())
Print 'OK'
SMTP. Close ()
Time. Sleep (1)

If _ name _ = "_ main __":
If Len (SYS. argv) <2:
Print 'usage: Python % s contentfilename '% OS. Path. basename (SYS. argv [0])
Print 'or usage: Python % s contentfilename attachfilename' % OS. Path. basename (SYS. argv [0])
Wait = raw_input ("quit .")
SYS. Exit (-1)
Elif Len (SYS. argv) = 2:
Sendfildbymail ({
'From': from,
'To':,
'Subobject': '[monitorcenter] Send MSG % s' % SYS. argv [1],
'Content': SYS. argv [1],
'Server': stmp_server,
'Port': stmp_port,
'Username': username,
'Password': userpassword })
Elif Len (SYS. argv) = 3:
Sendfildbymail ({
'From': from,
'To':,
'Subobject': '[monitorcenter] Send MSG and file % S % s' % (SYS. argv [1], SYS. argv [2]),
'Content': SYS. argv [1],
'File': SYS. argv [2],
'Server': stmp_server,
'Port': stmp_port,
'Username': username,
'Password': userpassword })
Wait = raw_input ("end .")
# Coding = UTF-8
#! /Usr/lib/python2.5/bin/Python
Import OS
Import sys
From smtplib import SMTP
From email. mimemultipart import mimemultipart
From email. Mime. Application import mimeapplication
From email. mimetext import mimetext
From email. mimebase import mimebase
From email import utils, encoders
Import mimetypes
Import time

Stmp_server = "mail. ×××. com"
Stmp_port = "25"
Username = "XXX @ xxx. com"
Userpassword = "×××"
From = "monitorcenterwarning @ ×××. com"
To = "××× @ gmail.com"

Def sendfildbymail (config ):
Print 'preparing ...'
Message = mimemultipart ()
Message ['from'] = config ['from']
Message ['to'] = config ['to']
Message ['reply-to'] = config ['from']
Message ['subobject'] = config ['subobject']
Message ['date'] = time. ctime (Time. Time ())
Message ['x-priority '] = '3'
Message ['x-msmail-priority '] = 'normal'
Message ['x-mailer '] = 'Microsoft Outlook Express 6.00.2900.2180'
Message ['x-mimeole'] = 'produced by Microsoft mimeole v6.00.2900.2180'

If 'file' in config:
# Add attachment
F = open (config ['file'], 'rb ')
File = mimeapplication (F. Read ())
F. Close ()
File. add_header ('content-disposition', 'attachment', filename = OS. Path. basename (config ['file'])
Message. Attach (file)

If 'content' in config:
# Add text content
F = open (config ['content'], 'rb ')
F. Seek (0)
Content = f. Read ()
Body = mimetext (content, 'base64', 'gb2312 ')
Message. Attach (Body)

Print 'okay'
Print 'logging ...'
SMTP = SMTP (config ['server'], config ['Port'])
# Add comments to the following line if the SMTP server does not need to verify logon when sending an email
SMTP. login (config ['username'], config ['Password'])
Print 'OK'

Print 'sending ...',
SMTP. Sendmail (config ['from'], [config ['from'], config ['to'], message. as_string ())
Print 'OK'
SMTP. Close ()
Time. Sleep (1)

If _ name _ = "_ main __":
If Len (SYS. argv) <2:
Print 'usage: Python % s contentfilename '% OS. Path. basename (SYS. argv [0])
Print 'or usage: Python % s contentfilename attachfilename' % OS. Path. basename (SYS. argv [0])
Wait = raw_input ("quit .")
SYS. Exit (-1)
Elif Len (SYS. argv) = 2:
Sendfildbymail ({
'From': from,
'To':,
'Subobject': '[monitorcenter] Send MSG % s' % SYS. argv [1],
'Content': SYS. argv [1],
'Server': stmp_server,
'Port': stmp_port,
'Username': username,
'Password': userpassword })
Elif Len (SYS. argv) = 3:
Sendfildbymail ({
'From': from,
'To':,
'Subobject': '[monitorcenter] Send MSG and file % S % s' % (SYS. argv [1], SYS. argv [2]),
'Content': SYS. argv [1],
'File': SYS. argv [2],
'Server': stmp_server,
'Port': stmp_port,
'Username': username,
'Password': userpassword })
Wait = raw_input ("end .")
 

In Windows XP:

 

For Linux Ubuntu and Suse:

 

Result received:

 

 

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.