import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText class SendEmail: """郵件方式發送報告""" global send_user global email_host global password send_user = "AAAAA@163.com" email_host = "smtp.163.com" password = 'CCCC' def send_mail(self, user_list, sub, content): """ 發送郵件 :param user_list: 收件人列表 :param sub: 主題 :param content: 內容 :return: """ user = "zhangweixu1379@163.com" + "<" + send_user + ">" message = MIMEMultipart() message['Subject'] = sub message['From'] = user message['To'] = ",".join(user_list) body = MIMEText(content, _subtype="plain", _charset="utf-8") # 郵件內容 message.attach(body) # 添加附件 att = MIMEText(open("../dataconfig/case1.xls", "rb").read(), "base64", "utf-8") # 打開附件地址 att["Content-Type"] = "application/octet-stream" att["Content-Disposition"] = 'attachment; filename="case1.xls"' message.attach(att) server = smtplib.SMTP() server.connect(email_host) server.login(send_user, password) server.sendmail(user, user_list, message.as_string()) server.close() def send_main(self, pass_list, fail_list): """ 發送報告 :param pass_list: :param fail_list: :return: """ pass_num = float(len(pass_list)) fail_num = float(len(fail_list)) count_num = pass_num + fail_num pass_result = "%.2f%%" % (pass_num / count_num * 100) fail_result = "%.2f%%" % (fail_num / count_num * 100) user_list = ['XXXX@dingtalk.com', 'XXX@dingtalk.com'] sub = '接口自動化測試報告' content = f"這次接口測試一共運行用例爲:{count_num},經過個數爲:{pass_num},失敗個數爲:{fail_num},經過率爲:{pass_result},報告詳情請查看附件!" self.send_mail(user_list, sub, content) if __name__ == '__main__': send = SendEmail() send.send_main([1, 2, 3, 4], [6, 2])