Selenium+unittest(5)傳送郵件
阿新 • • 發佈:2020-09-14
email_util.py郵件類。用來給指定使用者傳送郵件。可指定多個收件人,可帶附件。
""" 郵件類。用來給指定使用者傳送郵件。可指定多個收件人,可帶附件。 """ import re import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from socket import gaierror, error from src.config.config import REPORT_FILE, RunConfig from src.util.log importlogger def send_email(report): system_name = RunConfig.system_name e = Email(title=system_name + '測試報告', message='這是今天的測試報告,請查收!', receiver='[email protected]', server='smtp.qq.com', sender='[email protected]', password='mlddgdjcmpuzbgca', path=report ) e.send() class Email: def __init__(self, server, sender, password, receiver, title, message=None, path=None): """初始化Email :param title: 郵件標題,必填。 :param message: 郵件正文,非必填。 :param path: 附件路徑,可傳入list(多附件)或str(單個附件),非必填。 :param server: smtp伺服器,必填。 :param sender: 發件人,必填。 :param password: 發件人密碼,必填。 :param receiver: 收件人,多收件人用“;”隔開,必填。""" self.title = title self.message = message self.files = path self.msg = MIMEMultipart('related') self.server = server self.sender = sender self.receiver = receiver self.password = password def _attach_file(self, att_file): """將單個檔案新增到附件列表中""" att = MIMEText(open('%s' % att_file, 'rb').read(), 'plain', 'utf-8') att["Content-Type"] = 'application/octet-stream' file_name = re.split(r'[\\|/]', att_file) att["Content-Disposition"] = 'attachment; filename="%s"' % file_name[-1] self.msg.attach(att) logger.info('attach file {}'.format(att_file)) def send(self): self.msg['Subject'] = self.title self.msg['From'] = self.sender self.msg['To'] = self.receiver # 郵件正文 if self.message: self.msg.attach(MIMEText(self.message)) # 新增附件,支援多個附件(傳入list),或者單個附件(傳入str) if self.files: if isinstance(self.files, list): for f in self.files: self._attach_file(f) elif isinstance(self.files, str): self._attach_file(self.files) # 連線伺服器併發送 try: smtp_server = smtplib.SMTP(self.server) # 連線sever except (gaierror and error) as e: logger.exception('傳送郵件失敗,無法連線到SMTP伺服器,檢查網路以及SMTP伺服器. %s', e) else: try: smtp_server.login(self.sender, self.password) # 登入 except smtplib.SMTPAuthenticationError as e: logger.exception('使用者名稱密碼驗證失敗!%s', e) else: smtp_server.sendmail(self.sender, self.receiver.split(';'), self.msg.as_string()) # 傳送郵件 logger.info('傳送郵件"{0}"成功! 收件人:{1}。如果沒有收到郵件,請檢查垃圾箱,' '同時檢查收件人地址是否正確'.format(self.title, self.receiver)) finally: smtp_server.quit() # 斷開連線 if __name__ == '__main__': send_email(REPORT_FILE)