python send mail

This article is mainly for the data in the statistical database, and the data is stored in Excel, and finally the Excel is sent to the specified user by email; directly on the code.

1. Log configuration file Logger.conf

############################################
[loggers]
keys = root,example01,example02
[logger_root]
level = DEBUG
handlers = hand01,hand02
[logger_example01]
handlers = hand01,hand02
qualname = example01
propagate = 0
[logger_example02]
handlers = hand01,hand03
qualname = example02
propagate = 0
############################################
[handlers]
keys = hand01,hand02,hand03
[handler_hand01]
class = StreamHandler
level = DEBUG
formatter = form01
args = (sys.stderr,)
[handler_hand02]
class = FileHandler
lever = DEBUG
formatter = form01
args = ('./Result_count.log','a')
[handler_hand03]
class = handlers.RotatingFileHandler
lever = INFO
formatter = form01
args = ('./Result_count.log','a',10*1024*1024,5)
############################################
[formatters]
keys = form01,form02
[formatter_form01]
format = %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt = %Y-%m-%d %H:%M:%S
[formatter_form02]
format = %(name) -12s: %(levelname) -8s %(message)s
datefmt = %Y-$m-%d %H:%M:%S

Second, the program to read the configuration file: Log.py

#!/usr/bin/python
#encoding=utf-8
import logging.config
logging.config.fileConfig("Logger.conf")

def debug(message):
logging.debug(message)

def warning(message):
logging.warning(message)

def info(message):
logging.info(message)

3. Connect to the database and save the statistical results as Excel

#!/usr/bin/python 
# -*- coding:utf-8 -*-
'''
method: connect the mysql database through the pymsql module, and then query the SQL statement through the cursor cursor to store the result in the Excel file, where the Excel's Generated using xlwt.
Author: Mr. Fan
Time: April 2018
'''
import pymysql
import xlwt
import datetime
from Log import *


def Select_data():
conn = pymysql.connect(host='xxxx', port=xxx, user='root', passwd='PxxxxxxxSH', ​​db='xx', charset='utf8')
logging.info(u"#The connection to the mysql database is successful!")
logging.info(u"#Use the cursor() method to get the operation cursor;")
cursor = conn.cursor()
logging.info(u"#Use the execute method to execute the SQL statement and store the statistical results in the effect_row variable;")
today = datetime.datetime.today() #Get today's date.

tomorrow = today + datetime.timedelta(days=1) #Get the date of tomorrow.
today_a = datetime.datetime(today.year, today.month, today.day, 0, 0, 0) #Get the time in the early morning today.
yesterday_b = datetime.datetime(yesterday.year, yesterday.month, yesterday.day, 0, 0, 0)#Get the time of yesterday morning.
tomorrow_c = datetime.datetime(tomorrow.year, tomorrow.month, tomorrow.day, 0, 0, 0) #Get the time in the early morning of tomorrow.
#Format the time output, which is used when naming Excel.
sheet_time = datetime.datetime.now()
book_mark = sheet_time.strftime('%Y%m%d') #Store
the statistical results in the variable effect_row.
effect_row = cursor.execute("select a.username,a.mac,count(if(b.ntype='xx',true,null)),count(if(b.ntype='xx',true,null) ),count(if(b.ntype='xx',true,null)),count(if(b.ntype='xx',true,null)),count(if(b.ntype='xx', true,null)),count(if(b.ntype='xx',true,null)),count(if(b.ntype='xx',true,null)),count(if(b.ntype= 'xx',true,null)),count(if(b.ntype='xx',true,null)) from nctermnetlog_if_%sa,ncsnettype b where a.nettype=b.ntype and stime>unix_timestamp('%s ') and stime<unix_timestamp('%s') group by a.username"%(book_mark[0:6],str(yesterday_b),str(today_a))) #Get
a list of all records, that is, the total number of rows.
#print effect_row #Print the total number of rows . #Get
statistics and store the results in row_3.
row_3 = cursor.fetchall()
#print row_3 #Print statistics.
#Get the name of the retrieval condition in the above SQL statement (will become the header of the first row of Excel).
fields = cursor.description
logging.info(u"#Do submit request and close disconnection in turn;")
conn.commit()
cursor.close()
conn.close()
return row_3,book_mark,fields

def WritToExcel():
Data = Select_data()
logging.info(u"#Write the field name to the EXCEL header;")
workbook = xlwt.Workbook(encoding='utf-8') #Create
a sheet in Excel and name it Overridable state.
sheet = workbook.add_sheet('result_count',cell_overwrite_ok=True)
#Construct a list VnameList for renaming the above table headers, which must be in one-to-one correspondence.
VnameList = [u"Username","MAC",u"WeChat ID","QQ",u"Sina Weibo",u"Tencent Weibo",u"Tencent Video",u"Jingdong Mall",u "Taobao",u"Today's Toutiao",u"Meituan"]
logging.info(u"

sheet.write(0,field,VnameList[field].encode("utf-8")) #Enter

the queried information values ​​in turn according to the horizontal and vertical coordinates.
row = 1
col = 0
for row in range(1,len(Data[0])+1):
for col in range(0,len(Data[2])):
sheet.write(row,col,u' %s'%Data[0][row-1][col])
logging.info(u"#Save the Excel file;")
logging.info(u"#Save successfully!!!\n")
workbook. save('./Count_result%s.xls'%Data[1].encode("utf-8"))

if __name__=="__main__":
WritToExcel()

4. Mail sending program

#!/usr/bin/python 
# -*- coding:utf-8 -*-
'''
method: After successful login through the 163 mailbox of SNMP protocol, read the attachment in the specified location, and send the attachment to the designated recipient .
Author: Mr. Fan
Time: April 2018
'''
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
import time
import datetime
from Log import *
import ConMySqlOut
ConMySqlOut.WritToExcel()
time.sleep(5)
# print "*"*60

def SendMail():
smtpserver = 'smtp.163.com'
username = '[email protected]'
password='xxxxxx ' #authorization code
sender='
logging.info(u"# Log in to the mailbox server successfully;")

receiver=['[email protected]','[email protected]'] #Generate
today's date, format the output as year, month, day; generate yesterday's Date, formatted as year month day
sheet_time = datetime.datetime.now()
book_mark = sheet_time.strftime('%Y%m%d')
today = datetime.datetime.today()
yesterday = today - datetime.timedelta( days=1)
yesterday_b = datetime.datetime(yesterday.year, yesterday.month, yesterday.day, 0, 0, 0)
book_mark1 = yesterday_b.strftime('%Y%m%d')

logging.info(u"# Construct the subject, sender and recipient information of the email; ")
subject = "%s virtual identity statistics results, please pay attention to check!"%book_mark1
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg ['From']="Mr. Fan's 163 mailbox<[email protected]>"
msg['To'] = ";".join(receiver)

logging.info(u"#Construct attachment;")
sendfile=open('./Count_result%s.xls'%book_mark,'rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8 ')
text_att["Content-Type"] = 'application/octet-stream'
text_att.add_header('Content-Disposition', 'attachment', filename='Count_result%s.xls'%book_mark1)
msg.attach(text_att)
logging.info(u"#Constructed successfully, ready to send mail!")
#==================================== ===================================================== =============================
# Send mail; considering that the server will treat the mail as spam, the mail fails to be sent and returns 554 , so an infinite loop is made until the sending is successful.
#==================================================== ===================================================== ===========
try:


smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
#print "Send Success!!!"
logging.warning(u"#邮件发送成功!!!")
break
except Exception as err:
print 'Sending Mail Failed:{0}'.format(err)
logging.warning('Sending Mail Failed:{0}'.format(err))
Failure_count+=1
info('Send Failure counts are %s'%Failure_count)
# continue
finally:
smtp.quit()

if __name__ == "__main__":
SendMail()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324518505&siteId=291194637