python+SMTP發送郵件測試報告

發郵件須要用到python兩個模塊,smtplib和email,這倆模塊是python自帶的,只需import便可使用。smtplib模塊主要負責發送郵件,email模塊主要負責構造郵件。其中MIMEText()定義郵件正文,Header()定義郵件標題。MIMEMulipart模塊構造帶附件。

第一種發送html格式郵件:html

  1. import smtplib  
  2. from email.mime.text import MIMEText       #MIMEText()定義郵件正文  
  3. from email.header import Header            #Header()定義郵件標題  
  4. #發送郵箱服務器  
  5. smtpserver = 'smtp.sina.com'  
  6. #發送郵箱用戶/密碼(登陸郵箱操做)  
  7. user = "username@sina.com"  
  8. password = "password"  
  9. #發送郵箱  
  10. sender = "username@sina.com"  
  11. #接收郵箱  
  12. receiver = "8888@qq.com"  
  13. #發送主題  
  14. subject = 'email by  python'  
  15. #編寫HTML類型的郵件正文(把HTML代碼寫進入)  
  16. msg = MIMEText('<html><body><a href="">百度一下</a></p></body></html>','html','utf-8')  
  17. msg['Subject'] = Header(subject,'utf-8')  
  18. #鏈接發送郵件<strong><span style="color:#ff6666;">(smtplib模塊基本使用格式)</span></strong>  
  19. smtp = smtplib.SMTP()  
  20. smtp.connect(smtpserver)  
  21. smtp.login(user,password)  
  22. smtp.sendmail(sender,receiver,msg.as_string())  
  23. smtp.quit()  
  24. 說明:
    smtplib.SMTP():實例化SMTP()
    connect(host,port):
    host:指定鏈接的郵箱服務器。
    port:指定鏈接服務器的端口號,默認爲25.
    login(user,password):user:登陸郵箱的用戶名。password:登陸郵箱的密碼。
    sendmail(from_addr,to_addrs,msg,...):
    from_addr:郵件發送者地址
    to_addrs:郵件接收者地址。字符串列表['接收地址1','接收地址2','接收地址3',...]或'接收地址'
    msg:發送消息:郵件內容。通常是msg.as_string(),as_string()是將msg(MIMEText對象或者MIMEMultipart對象)變爲str。
    quit():用於結束SMTP會話。

第二種發送帶附件的郵箱:html5

  1. import smtplib  
  2. from email.mime.text import MIMEText            #MIMRText()定義郵件正文  
  3. from email.mime.multipart import MIMEMultipart  #MIMEMulipart模塊構造帶附件  
  4. #發送郵件的服務器  
  5. smtpserver = 'smtp.sina.com'  
  6. #發送郵件用戶和密碼  
  7. user ="xxx@sina.com"  
  8. password = "xxx"  
  9. #發送者  
  10. sender = "xxx@sina.com"  
  11. #接收者  
  12. receiver = "1xxx@qq.com"  
  13. #郵件主題  
  14. subject = "附件的郵件"  
  15. #發送附件  
  16. sendfile = open("C:\\Users\\Administrator\\Desktop\\html5.txt","r").read()  
  17. att = MIMEText(sendfile,"base64","utf-8")  
  18. att["Content-Type"] = "application/octet-stream"  
  19. att["Content-Disposition"] = "attachment;filename = 'html5.txt'"  
  20. msgRoot = MIMEMultipart('related')  
  21. msgRoot['Subject'] = subject  
  22. msgRoot.attach(att)  
  23. smtp = smtplib.SMTP()  
  24. smtp.connect(smtpserver)  
  25. smtp.login(user,password)  
  26. smtp.sendmail(sender,receiver,msgRoot.as_string())  
  27. smtp.quit()  

第三種整合自動化測試發送測試報告郵件python

  1. from HTMLTestRunner import HTMLTestRunner  
  2. from email.mime.text import MIMEText  
  3. from email.header import Header  
  4. import smtplib  
  5. import unittest  
  6. import time  
  7. import os  
  8. #==============定義發送郵件==========  
  9. def send_mail(file_new):  
  10.     f = open(file_new,'rb')  
  11.     mail_body = f.read()  
  12.     f.close()  
  13.     msg = MIMEText(mail_body,'html','utf-8')  
  14.     msg['Subject'] = Header("自動化測試報告",'utf-8')  
  15.     smtp = smtplib.SMTP()  
  16.     smtp.connect('smtp.sina.com')                                      #郵箱服務器  
  17.     smtp.login("sender@sina.com","password")                           #登陸郵箱  
  18.     smtp.sendmail("sender@sina.com","receiver@qq.com",msg.as_string()) #發送者和接收者  
  19.     smtp.quit()  
  20. print("郵件已發出!注意查收。")  
  21. #======查找測試目錄,找到最新生成的測試報告文件======  
  22. def new_report(test_report):  
  23.     lists = os.listdir(test_report)                                    #列出目錄的下全部文件和文件夾保存到lists  
  24.     lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn))#按時間排序  
  25.     file_new = os.path.join(test_report,lists[-1])                     #獲取最新的文件保存到file_new  
  26. print(file_new)  
  27. return file_new  
  28. if __name__ == "__main__":  
  29.     test_dir = "測試用例存放目錄"  
  30.     test_report = "測試報告存放目錄"  
  31.     discover = unittest.defaultTestLoader.discover(test_dir,  
  32.                                                    pattern = 'test_*.py')  
  33.     now = time.strftime("%Y-%m-%d_%H-%M-%S")  
  34.     filename = test_report + '\\' + now + 'result.html'  
  35.     fp = open(filename,'wb')  
  36.     runner = HTMLTestRunner(stream = fp,  
  37.                             title = '測試報告',  
  38.                             description = '用例執行狀況:')  
  39.     runner.run(discover)  
  40.     fp.close()  
  41.     new_report = new_report(test_report)  
  42.     send_mail(new_report)     #發送測試報告  

1.經過unittest框架的discover()找到匹配的測試用例,由HTMLTestRunner的run()方法執行測試用例並生成最新的測試報告。服務器

2.調用new_report()函數找到測試報告目錄下最新生成的測試報告,返回測試報告的路徑。app

3.將獲得的最新測試報告的完整路徑傳給send_mail()函數,實現發郵件功能框架

相關文章
相關標籤/搜索