# -*- coding: utf-8 -*-
# @Time : 2021/10/28 11:16
# @Author : wangyinghao
# @File : send_email.py
# @Software: PyCharm
import smtplib
from email.mime.text import MIMEText


class SendEmail:
    global send_user
    global email_host
    global password
    email_host = "smtp.exmail.qq.com"
    send_user = "opit@qike366.cn"
    password = "0b*Ymsk!H1^aft1Qx955"

    # 接受人，主题，内容
    def send_mail(self, user_list, sub, content):
        # 发件者
        user = "opit" + "<" + send_user + ">"
        # 内容、格式、编码
        message = MIMEText(content, _subtype='plain', _charset='utf-8')
        # 主题
        message['Subject'] = sub
        # 发送人
        message['From'] = user
        msg_to = user_list.split(",")
        # 接受人
        message['To'] = ",".join(msg_to)
        # 服务连接
        server = smtplib.SMTP()
        server.connect(email_host)
        # 邮件登录
        server.login(send_user, password)
        # 发送邮件内容
        server.sendmail(user, msg_to, message.as_string())
        # 关掉连接
        server.close()

    def send_main(self, pass_list, fail_list):
        pass_num = float(len(pass_list))
        fail_num = float(len(fail_list))
        count_num = pass_num + fail_num
        # 取小数后2位,通过率
        pass_result = "%.2f%%" % (pass_num / count_num * 100)
        fail_result = "%.2f%%" % (fail_num / count_num * 100)
        user_list = "979897776@qq.com,15810937212@qq.com"
        sub = "接口自动化测试报告"
        content = "这次一共测试%s个接口,通过个数为%s,失败个数为%s,通过率为%s,失败率为%s" % (
            count_num, pass_num, fail_num, pass_result, fail_result)
        self.send_mail(user_list, sub, content)


if __name__ == '__main__':
    sen = SendEmail()
    sen.send_main([1, 2, 3, 4], [5])
