小編給大家分享一下怎么使用python將最新的測試報告以附件的形式發(fā)到指定郵箱,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)專注于嘉黎企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,商城網(wǎng)站建設(shè)。嘉黎網(wǎng)站建設(shè)公司,為嘉黎等地區(qū)提供建站服務(wù)。全流程定制開發(fā),專業(yè)設(shè)計,全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)python主要應(yīng)用領(lǐng)域有哪些1、云計算,典型應(yīng)用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應(yīng)用,基于大數(shù)據(jù)分析和深度學(xué)習(xí)而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運(yùn)維工程項(xiàng)目,自動化運(yùn)維的標(biāo)配就是python+Django/flask。5、金融理財分析,量化交易,金融分析。6、大數(shù)據(jù)分析。
具體代碼如下所示:
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'] #單個收件人 # receivers = ['xxx@dadaodata.com', 'xxx@qq.com'] # 收件人地址或是地址列表,支持多個收件人,最多30個 # 構(gòu)造郵件MIMEMultipart對象 msg = MIMEMultipart('mixed') msg['Subject'] = Header('自動化測試報告' + 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('附件為接口自動化測試報告,請查收!', '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)) #按時間排序 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)證錯誤:', 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é)果如下:
看完了這篇文章,相信你對“怎么使用python將最新的測試報告以附件的形式發(fā)到指定郵箱”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享名稱:怎么使用python將最新的測試報告以附件的形式發(fā)到指定郵箱-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://www.chinadenli.net/article0/dhchoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、網(wǎng)站排名、軟件開發(fā)、企業(yè)網(wǎng)站制作、網(wǎng)站營銷、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容