zoukankan      html  css  js  c++  java
  • python 给多人发送邮件,且将结果添加为附件

    import unittest,HTMLTestRunner
    import os
    def runa():
    path=os.getcwd()
    print(path)
    a=unittest.defaultTestLoader.discover(path,
    pattern='login*.py')
    al=unittest.TestSuite()
    al.addTest(a)
    #print(al)
    return al

    import os,time
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    from email.utils import formataddr
    import smtplib
    def lu(path):
    filesn=os.listdir(path)
    filesn.sort(key=lambda x:os.path.getmtime(path+x))
    #luf=path+filesn[-1]
    luf=os.path.join(path+filesn[-1])
    return luf
    #print(lu("D:\\study\\python_api_test\\test1204\\"))
    def send_out(luf):
    x=open(luf,'rb')
    email=x.read()
    x.close()
    usernames='371933505@qq.com'
    passwd='kclpuvarbapocagj' #此处密码错误,运行时,修改为正确密码
    sender='371933505@qq.com'
    receiver=['371933505@qq.com','1059084854@qq.com']
    info=MIMEMultipart()
    info['From']=Header("测试人:慧慧<%s>"%sender,'utf-8')
    info['To']=Header("请老板们查阅<%s>"%receiver,'utf-8')
    info['Subject']=Header('这是python自动化测试报告...','utf-8')
    info.attach(MIMEText(email,'html','utf-8'))
    attach1=MIMEText(open(luf,'rb').read(),'base64','utf-8')
    attach1['Content-Type']='application/octet-stream'
    attach1["Content-Disposition"]='attachment;filename="result.html"'
    info.attach(attach1)
    smtp=smtplib.SMTP_SSL("smtp.qq.com",465)
    smtp.login(usernames,passwd)
    smtp.sendmail(sender,receiver,info.as_string())
    smtp.quit()
    print("邮件已发出!请注意查收!")


    if __name__=="__main__":
    #unittest.TextTestRunner().run(runa())
    htmlrun=unittest.TextTestRunner()
    result=os.path.join(os.getcwd()+"\\result.html") #无result.html,则会自动创建
    print(result)
    a=open(result,'wb')
    htmlrun=HTMLTestRunner.HTMLTestRunner(stream=a,
    title='自动化测试结果',
    description='具体结果如下:',
    verbosity=2)
    htmlrun.run(runa())
    a.close()
    path="D:\\study\\python_api_test\\test1204\\"
    hehe=lu(path)
    send_out(hehe)
    ======================================================================
    小结:
    1.python使用了
    a=['ab','ee']
    b=';'.join(a) #输出结果为ab;ee
    2.使用了email中的MIMEMutipart()
    附件att1
    info=MIMEMutipart()
    att1=MIMEText(open('**文件','rb').read(),'base64','utf-8')
    att1=['Content-Type"]='application/octet-stream'
    att1=['Content-Disposition']='attachment;filename="可随意命名"'
    info.attach(att1)
    Text1=MIMEText(open('**文件.html','rb').read())
    info.attach(Text1,'html','utf-8')
     
  • 相关阅读:
    angular 实现全选、反选、个别选择的实现
    idea下git版本回退
    统一设置滚动条样式
    vue小知识点
    Java面试题之Oracle 支持哪三种事务隔离级别
    Java面试题之hashmap中用什么hash算法解决碰撞的?
    Java面试题之Integer.valueOf(String s);采用了什么设计模式
    TCP面试题之为什么会有TIME_WAIT状态
    Java面试题之如何防止重复下单问题?
    Java面试题之在多线程情况下,单例模式中懒汉和饿汉会有什么问题呢?
  • 原文地址:https://www.cnblogs.com/canglongdao/p/12002708.html
Copyright © 2011-2022 走看看