python send a message to the people, and the result is added as an attachment

import unittest,HTMLTestRunner
import os
def runa():
path=os.getcwd()
print(path)
a=unittest.defaultTestLoader.discover(path,
pattern='login*.py')
al=unittest.TestSuite()
al.addTest(a)
#print(al)
return al

import os,time
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
import smtplib
def lu(path):
filesn=os.listdir(path)
filesn.sort(key=lambda x:os.path.getmtime(path+x))
#luf=path+filesn[-1]
= the os.path.join LUF (path + filesn [-1])
return LUF
#Print (LU ( "D: \\ \\ test1204 python_api_test Study \\ \\"))
DEF send_out (LUF):
X = Open ( LUF, 'rb')
Email = x.read ()
x.close ()
usernames='[email protected] '
passwd =' kclpuvarbapocagj '# here wrong password, run, change to the correct password
sender =' 371933505 @ qq.com '
Receiver = [' [email protected] ',' [email protected] ']
info = MimeMultipart ()
info [' the From '] = Header ( "test persons: Rati <% s>"% sender , 'UTF-. 8')
info [ 'the To'] = Header ( "Please bosses Now <% S>"% Receiver, 'UTF-. 8')
info [ 'the Subject'] = Header ( 'which is python automated testing report ... ',' UTF-. 8 ')
info.attach(MIMEText(email,'html','utf-8'))
attach1=MIMEText(open(luf,'rb').read(),'base64','utf-8')
attach1['Content-Type']='application/octet-stream'
attach1["Content-Disposition"]='attachment;filename="result.html"'
info.attach(attach1)
smtp=smtplib.SMTP_SSL("smtp.qq.com",465)
smtp.login(usernames,passwd)
smtp.sendmail(sender,receiver,info.as_string())
smtp.quit()
print("邮件已发出!请注意查收!")


if __name__=="__main__":
#unittest.TextTestRunner().run(runa())
htmlrun=unittest.TextTestRunner()
result=os.path.join(os.getcwd()+"\\result.html") #无result.html,则会自动创建
print(result)
a=open(result,'wb')
htmlrun=HTMLTestRunner.HTMLTestRunner(stream=a,
title = 'automated test results',
description = 'results are as follows:',
the verbosity = 2)
htmlrun.run (Runa ())
a.close ()
path = "D: \\ python_api_test Study \\ \\ \\ test1204"
Hehe LU = (path)
send_out (hehe)
=============================================== =======================
Summary:
1.python using
a = [ 'ab &', 'EE']
B = ';' the Join (a. ) # output is ab &; EE
2. using email for MIMEMutipart ()
Annex the ATTl
info = MIMEMutipart ()
. MimeText the ATTl = (Open ( '** file', 'rb') read ( ), 'base64', 'UTF-. 8')
the ATTl = [ 'the Type-the Content "] =' file application / OCTET-Stream '
the ATTl = [' the Content-Disposition '] =' Attachment;filename = "may be named" '
info.attach (the ATTl)
Text1=MIMEText(open('**文件.html','rb').read())
info.attach(Text1,'html','utf-8')
 

Guess you like

Origin www.cnblogs.com/canglongdao/p/12002708.html