SMTP自動傳送郵件功能程式碼
阿新 • • 發佈:2019-01-12
SMTP自動傳送郵件功能
我也是跟著各種部落格一步一步搜尋出來的,可能在設定郵箱的時候會有各種問題,請參考我的其他部落格進行設定
https://blog.csdn.net/ly021499/article/details/82423019
https://blog.csdn.net/ly021499/article/details/80943030
以下程式碼集成了生成測試報告和自動傳送郵件等功能,使用了新的測試報告模版,個人認為美觀一點,另附下載連結
連結:https://pan.baidu.com/s/1kwh7nAdc-vC0NbEK4PfODA
提取碼:j77w
#-*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import BSTestRunner # 注意此處我所使用的是BSTestRunner,部落格附下載連結 import unittest import time import os ''' _(\_/) ,((((^`\ (((( (6 \ ,((((( , \ ,,,_ ,((((( /"._ ,`, ((((\\ ,... ,(((( / `-.-' ))) ;' `"'"'""(((( ( ((( / ((( \ )) | | (( | . ' | )) \ _ ' `t ,.') ( | y;- -,-""'"-.\ \/ ) / ./ ) / `\ \ |./ ( ( / /' || \\ //'| || \\ _//'|| || )) |_/ || \_\ |_/ || `'" \_\ `'" ''' # ✎﹏₯㎕﹍﹍ 定義傳送郵件 ζั͡ޓއއއ๓º def send_mail(new_file): # 發信郵箱 sender = Sender # 收信郵箱 receiver = Receiver # 開啟郵件 efile = open(new_file, 'rb') email_body = efile.read() efile.close() # 例項化一個帶附件的示例 message = MIMEMultipart() # 定義郵件正文內容 message.attach(MIMEText(email_body, _subtype='html', _charset='utf-8')) # 定義擡頭 message['From'] = Sender message['To'] = Receiver # 定義郵件主題 message['Subject'] = EmailTitle # 定義傳送時間(不定義的可能有的郵件客戶端會不顯示傳送時間) message['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') att = MIMEText(email_body, _subtype='base64', _charset='utf-8') # 構造附件,傳送當前目錄下的 test.txt 檔案 att["Content-Type"] = 'application/octet-stream' # 這裡的filename可以任意寫,寫什麼名字,郵件中顯示什麼名字 att["Content-Disposition"] = 'attachment; filename=%s' %AttName message.attach(att) # 連線SMTP伺服器 smtp = smtplib.SMTP() smtp.connect(SMTP) # 使用者名稱密碼 smtp.login(Sender, EmailPassword) smtp.sendmail(sender, receiver, message.as_string()) smtp.quit() print('✎biubiubiu ₯㎕~~~ 導彈發射成功,顫抖吧!愚蠢的人類 ޓއއއ ') # ✎﹏₯㎕﹍﹍ 查詢最新生成的測試報告檔案 ζั͡ޓއއއ๓º def send_report(testreport): result_dir = testreport lists = os.listdir(result_dir) lists.sort(key=lambda fn: os.path.getmtime(result_dir + "\\" + fn)) print (u'最新的導彈已製成,即將進入發射程式: '+lists[-1]) # 找到最新生成的檔案 new_file = os.path.join(result_dir, lists[-1]) print(new_file) #呼叫發郵件模組 send_mail(new_file) # ✎﹏₯㎕﹍﹍ 將用例新增到測試套件 ζั͡ޓއއއ๓º def creatsuite(): testunit = unittest.TestSuite() # 定義測試檔案查詢的目錄 testcase_path = TestCasePath # 定義 discover 方法的引數 discover = unittest.defaultTestLoader.discover(testcase_path, pattern='test*.py',top_level_dir=None) # discover 方法篩選出來的用例,迴圈新增到測試套件中 for test_case in discover: print(test_case) testunit.addTests(test_case) return testunit def main(): now = time.strftime("%Y-%m-%d %H_%M") # 報告儲存的路徑 report_path = ReportPath # 構建檔案完整路徑 filename = report_path + ReportName + now + '.html' # 開啟報告檔案 file = open(filename, 'wb') # 設定報告頭部資訊 runner = BSTestRunner.BSTestRunner(stream=file, title=Title, description=Description) # 拿到測試套件並執行 all_test_case = creatsuite() runner.run(all_test_case) # 關閉生成的報告 file.close() # 傳送報告 send_report(ReportPath) if __name__ == '__main__': # 設定發信郵箱 Sender = '
[email protected]' # 發信郵箱賬號 # 設定收信郵箱 Receiver= '[email protected]' # 收信郵箱密碼 # 設定發信郵箱密碼 EmailPassword = 'xxxxxxx' # 此處填寫你在郵箱生成的客戶端專用密碼 # 設定郵件標題 EmailTitle = u"自動化測試報告" # 設定郵件正文內容 BodyContent = '此郵件為執行自動化指令碼後自動傳送郵件,請勿回覆!' # 設定郵件附件名稱 AttName = "TestReport.html" # 設定testCase儲存路徑 TestCasePath = os.path.dirname(os.path.abspath('.')) + '\\testcase' # 設定SMTP伺服器-(現在用的QQ郵箱伺服器) SMTP = "smtp.exmail.qq.com" # 設定報告的名字 ReportName = 'TestReport ' # 設定報告儲存的路徑 ReportPath = os.path.dirname(os.path.abspath('.')) + '\\reports\\' # 設定報告主題 Title = u'Big data platform' # 設定報告描述內容 Description = u'This is the automation test report of the big data platform for reference only.' # 呼叫main()方法啟動程式 main()