Python programming: sending mail based on monitoring results

Source: Internet
Author: User
Tags mail connect

The use of GG, need to monitor, their own write an automatic mail, this is just a prototype! According to the data returned from the database results are 1 or second to determine the synchronization situation,

and timely send mail and SMS notification and log!

#-*-coding:gb2312-*-

Import re

Import Pyodbc

Import Traceback

Import Decimal

Import OS, sys

Import time

Import Smtplib

From Email.mime.text import Mimetext

From Email.header Import Header

Sender = ' 1364188****@139.com '

Receiver = ' 1364188****@139.com '

Subject = ' Data Synchronization monitoring '

SmtpServer = ' smtp.139.com '

Username = ' 1364188**** '

Password = ' ************ '

####### #判断同步是否正常

guess = Int (input ("Please enter an integer: \ n"))

If guess = 1:

msg = Mimetext (' Monitor normal! ', ' plain ', ' gb2312 ') #中文需参数 ' gb2312 ', single-byte characters do not need

msg[' Subject ' = Header (Subject, ' gb2312 ')

msg[' Date ']=time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ())

SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.139.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msg.as_string ())

Logtxt= ' monitor Mail has been sent in ' +msg[' Date ']+ ' successfully! ' + ': Sync normal '

Smtp.quit ()

Print (Logtxt)

f = open (' E:\py\log.txt ', ' a ')

F.write (logtxt+ ' \ n ')

F.close ()

Print (' log: ' +logtxt+ ' record successful ')

Elif guess = = 2:

msg = Mimetext (' Monitor exception! ', ' plain ', ' gb2312 ') #中文需参数 ' gb2312 ', single-byte characters do not need

msg[' Subject ' = Header (Subject, ' gb2312 ')

msg[' Date ']=time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime ())

SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.139.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msg.as_string ())

Logtxt= ' monitor Mail has been sent in ' +msg[' Date ']+ ' successfully! ' + ': Sync exception '

Smtp.quit ()

Print (Logtxt)

f = open (' E:\py\log.txt ', ' a ')

F.write (logtxt+ ' \ n ')

F.close ()

Print (' log: ' +logtxt+ ' record successful ')

Else

Print (' ************** input type is wrong, please contact ocpyang!********************** ')

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.