1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2023 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18import os 19import smtplib 20 21from email.mime.image import MIMEImage 22from email.mime.multipart import MIMEMultipart 23from email.mime.text import MIMEText 24import yaml 25 26 27def add_content(content, file_name, test_part): 28 if file_name == "": 29 content += f'<p style="text-align:center;color:red;font-size:25px"> {test_part} not complete yet </p>' 30 return content 31 if not os.path.exists(file_name): 32 content += f'<p style="text-align:center;color:red;font-size:25px"> {test_part} run failed </p>' 33 return content 34 with open(file_name, 'r', encoding='utf-8') as f: 35 content += f.read() 36 return content 37 38 39def add_attachment(msg, file_list): 40 for file in file_list: 41 if os.path.exists(file): 42 with open(file, 'rb') as f: 43 attachment = MIMEText(f.read(), 'base64', 'utf-8') 44 attachment['Content-Disposition'] = f'attachment; filename="{os.path.basename(file)}"' 45 msg.attach(attachment) 46 47 48def add_image(msg, img_dic): 49 for path in img_dic: 50 if os.path.exists(path): 51 with open(path, 'rb') as f: 52 img = MIMEImage(f.read()) 53 img.add_header('Content-ID', img_dic[path]) 54 msg.attach(img) 55 56 57def send_email(): 58 with open(r".\email_config.yaml", 'r') as f: 59 data = yaml.safe_load(f.read()) 60 61 user_name = data["user_name"] 62 sender = data["sender_email_address"] 63 auth_code = data["auth_code"] 64 receiver = data["receiver_list"] 65 smtp_server = data["smtp_server"] 66 smtp_port = data["smtp_port"] 67 xts_test = data["xts_report_file"] 68 sdk_test = data["sdk_report_file"] 69 perf_test = data["perf_report_file"] 70 attachment_files = data["attatchment_files"] 71 image_files = data["image_files"] 72 73 msg = MIMEMultipart() 74 msg['From'] = sender 75 msg['To'] = ", ".join(receiver) 76 msg['Subject'] = "Arkcompiler Test" 77 78 html = "" 79 dividing_line = '<hr align="center" width="80%" color="gray" size="8">' 80 html = add_content(html, xts_test, "xts_test") 81 html += dividing_line 82 html = add_content(html, sdk_test, "sdk_test") 83 html += dividing_line 84 html = add_content(html, perf_test, "perf_test") 85 msg.attach(MIMEText(html, 'html', 'utf-8')) 86 add_attachment(msg, attachment_files) 87 add_image(msg, image_files) 88 smtp = smtplib.SMTP(smtp_server, smtp_port) 89 smtp.login(user_name, auth_code) 90 smtp.sendmail(sender, receiver, msg.as_string()) 91 smtp.quit() 92 93 94if __name__ == "__main__": 95 send_email()