黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

使用python將最新的測試報告以附件的形式發(fā)到指定郵箱

系統(tǒng) 1900 0

具體代碼如下所示:

            
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é)果如下:

總結(jié)

以上所述是小編給大家介紹的使用python將最新的測試報告以附件的形式發(fā)到指定郵箱,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論