首页 > 编程语言 > 详细

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

时间:2019-03-16 00:50:11      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:smtplib   oca   efault   %s   alt   ica   python   har   subject   

# 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
        #如果多个接受者用列表 [‘[email protected]‘,‘[email protected]‘]
        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

 

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

标签:smtplib   oca   efault   %s   alt   ica   python   har   subject   

原文:https://www.cnblogs.com/SunshineKimi/p/10540182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号