使用python將最新的測試報(bào)告以附件的形式發(fā)到指定郵箱
具體代碼如下所示:
import smtplib, email, os, time from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header #設(shè)置smtplib所需的參數(shù) smtpserver = 'smtp.qq.com' #SMTP服務(wù)器地址 username = 'xxx@qq.com' # 發(fā)件人地址,通過控制臺創(chuàng)建的發(fā)件人地址 password = '******' # 發(fā)件人密碼,通過控制臺創(chuàng)建的發(fā)件人密碼 receiver = ['xxx@dadaodata.com'] #單個(gè)收件人 # receivers = ['xxx@dadaodata.com', 'xxx@qq.com'] # 收件人地址或是地址列表,支持多個(gè)收件人,最多30個(gè) # 構(gòu)造郵件MIMEMultipart對象 msg = MIMEMultipart('mixed') msg['Subject'] = Header('自動(dòng)化測試報(bào)告' + time.strftime("%Y-%m-%d"), 'utf-8').encode()#自定義郵件主題 msg['From'] = '%s <%s>' % (username, username)#郵件發(fā)送者 msg['To'] = ";".join(receiver)#郵件接受者 msg['Message-id'] = email.utils.make_msgid() msg['Date'] = email.utils.formatdate() # 構(gòu)造文字內(nèi)容 text_plain = MIMEText('附件為接口自動(dòng)化測試報(bào)告,請查收!', 'plain', 'utf-8')#郵件內(nèi)容 msg.attach(text_plain) #構(gòu)造附件 test_report = r'F:\PythonAutomation\Python_PyCharm\TestReport' #存放文件的目錄 lists = os.listdir(test_report) #列出目錄的下所有文件保存到lists lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn)) #按時(shí)間排序 file_new = os.path.join(test_report,lists[-1]) #獲取最新的文件保存到file_new sendfile = open(file_new,'rb').read() text_att = MIMEText(sendfile, 'base64', 'utf-8') text_att["Content-Type"] = 'application/octet-stream' text_att["Content-Disposition"] = 'attachment; filename="report.html"'#重新命名附件 msg.attach(text_att) # 發(fā)送郵件 try: # client = smtplib.SMTP() # client.connect(smtpserver, 25) #SMTP普通端口為25 client = smtplib.SMTP_SSL() #python 2.7以上版本,若需要可使用SSL client.connect(smtpserver, 465) #SSL端口465 # client.set_debuglevel(1) #用set_debuglevel(1)可以打印出和SMTP服務(wù)器交互的所有信息 client.login(username, password) client.sendmail(username, receiver, msg.as_string()) client.quit() print('郵件發(fā)送成功') except smtplib.SMTPConnectError as e: print('郵件發(fā)送失敗,連接失敗:', e.smtp_code, e.smtp_error) except smtplib.SMTPAuthenticationError as e: print('郵件發(fā)送失敗,認(rèn)證錯(cuò)誤:', e.smtp_code, e.smtp_error) except smtplib.SMTPSenderRefused as e: print('郵件發(fā)送失敗,發(fā)件人被拒絕:', e.smtp_code, e.smtp_error) except smtplib.SMTPRecipientsRefused as e: print('郵件發(fā)送失敗,收件人被拒絕:', e.args, e.recipients) except smtplib.SMTPDataError as e: print('郵件發(fā)送失敗,數(shù)據(jù)接收拒絕:', e.smtp_code, e.smtp_error) except smtplib.SMTPException as e: print('郵件發(fā)送失敗: ', str(e)) except Exception as e: print('郵件發(fā)送失敗: ', str(e))
執(zhí)行結(jié)果如下:
總結(jié)
以上所述是小編給大家介紹的使用python將最新的測試報(bào)告以附件的形式發(fā)到指定郵箱,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
總結(jié)Python函數(shù)參數(shù)的六種類型
這篇文章主要總結(jié)了Python函數(shù)參數(shù)的六種類型,傳遞參數(shù)實(shí)現(xiàn)不同場景的靈活使用,下面總結(jié)的六種函數(shù)參數(shù)類型,需要的小伙伴可以參考一下2022-03-03圖文講解選擇排序算法的原理及在Python中的實(shí)現(xiàn)
這篇文章主要介紹了選擇排序的原理及在Python中的實(shí)現(xiàn),選擇排序的時(shí)間復(fù)雜度為О(n²),需要的朋友可以參考下2016-05-05keras實(shí)現(xiàn)多GPU或指定GPU的使用介紹
這篇文章主要介紹了keras實(shí)現(xiàn)多GPU或指定GPU的使用介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python實(shí)現(xiàn)的ini文件操作類分享
這篇文章主要介紹了Python實(shí)現(xiàn)的ini文件操作類分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-11-11Python實(shí)現(xiàn)交通數(shù)據(jù)可視化的示例代碼
本文主要分享了Python交通數(shù)據(jù)分析與可視化的實(shí)戰(zhàn)!其中主要是使用TransBigData庫快速高效地處理、分析、挖掘出租車GPS數(shù)據(jù),感興趣的可以了解一下2023-04-04Requests什么的通通爬不了的Python超強(qiáng)反爬蟲方案!
今天帶大家學(xué)習(xí)Requests什么的通通爬不了的Python超強(qiáng)反爬蟲方案,文中有非常詳細(xì)的圖文介紹及代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05