zoukankan      html  css  js  c++  java
  • python+SMTP发送邮件测试报告

    发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Header()定义邮件标题。MIMEMulipart模块构造带附件。

    第一种发送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会话。

    第二种发送带附件的邮箱:

    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()  

    第三种整合自动化测试发送测试报告邮件

    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()函数找到测试报告目录下最新生成的测试报告,返回测试报告的路径。

    3.将得到的最新测试报告的完整路径传给send_mail()函数,实现发邮件功能

  • 相关阅读:
    C# 高级编程语言
    unity ForceMode
    UnityError 切换场景灯光变黑问题解决
    Unity Time.timeScale
    Unity 打开网页 Application.OpenURL(字符串);
    Unity www动态加载网上图片
    Unity GameObject.Find 和 transform.Find
    Unity UGUI按钮添加点击事件
    事务
    git和redis
  • 原文地址:https://www.cnblogs.com/xqh1215/p/9585790.html
Copyright © 2011-2022 走看看