python 自动化实现定时发送html报告到邮箱

# coding =utf-8
import os
import unittest
import time
import datetime
import smtplib
from email.mime.text import MIMEText
from HTMLTestRunner import HTMLTestRunner


def all_cases():
        case_path = os.getcwd()

        discover = unittest.defaultTestLoader.discover(case_path, pattern='test*.py', top_level_dir=None)
        return discover


def run(report_path):
        with open(report_path, 'wb') as f:
            runner = HTMLTestRunner(stream=f, title="interface report", description="results like following:", verbosity=2)
            runner.run(all_cases())
        f.close()


def timer(report_path, hour, minute):
    flag = 1
    while flag:
        while flag:
            now = datetime.datetime.now()
            if now.hour == hour and now.minute == minute:
                break
            time.sleep(10)
        # run(report_path)
        email_send(report_path)
        flag = 0


def email_send(report_path,str_sender,receiver,author_code,smtp_server="smtp.qq.com",smtp_int_port=465):
        run(report_path)
        msg_from = str_sender  # sender
        passwd = author_code     # authentication  code
        msg_to = receiver    # receiver
        #如果多个接受者用列表 ['1932390299@qq.com','1393232463@qq.com']
        subject = "python_email_test"
        f = open(report_path, 'rb')
        mail_body = f.read()
        f.close()
        msg=MIMEText(mail_body, _subtype='html', _charset='utf-8')

        msg['Subject'] = subject
        msg['From'] = msg_from
        msg['To'] = msg_to
        try:
            s = smtplib.SMTP_SSL(smtp_server, smtp_int_port)
            s.login(msg_from, passwd)
            s.sendmail(msg_from, msg_to, msg.as_string())
            print("send success")
        except smtplib.SMTPException as e:
            print("send fail")
        finally:
            s.quit()

def get_report_path():
        job_name = time.strftime('job_%Y%m%d%H%M%S', time.localtime()) + '.html'
        path = os.path.join(os.path.dirname(__file__), 'report')
        report_path = os.path.join(path, job_name)
        return  report_path

 上面是很久以前的没更新:邮件发送见最新的文章封装很全面写了一次https://www.cnblogs.com/SunshineKimi/p/10629267.html

定时计划参考最新的机制schedule文章  :https://www.cnblogs.com/SunshineKimi/p/10630784.html

报告选型见我的报告模块:https://www.cnblogs.com/SunshineKimi/category/1426942.html

转载于:https://www.cnblogs.com/SunshineKimi/p/10540182.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值