获取授权码

设置—账户—获取授权码image.png
image.png

  1. # 参数1:收件人邮箱
  2. # 参数2:发送的内容
  3. # subject:邮件主题
  4. # content_text:邮件正文
  5. # attachments:邮件附件
  6. # 参数3:cc,可选,抄送
  7. s.send_mail("******@qq.com",{
  8. "subject":"最新测试结果报告",
  9. "content_text":"您好,\n 这是最新测试结果报告,请查收附件!",
  10. "attachments":"E:\\sProject1\\report\\测试结果报告.html"},
  11. cc="******@qq.com")
  1. # 导入zmail
  2. # 需要安装:pip install zmail==0.2.8 python3才能用
  3. import zmail
  4. # 导入日志处理模块
  5. from utils.logger import log
  6. from utils.read_mailconf import MailConf
  7. # 邮件工具类
  8. class EmailUtil:
  9. # 参数是要发送的测试结果报告文件路径
  10. def send_email(self, file_path):
  11. try:
  12. mail_host = MailConf().get_mail_host()
  13. mail_port = MailConf().get_mail_port()
  14. mail_username = MailConf().get_mail_username()
  15. mail_password = MailConf().get_mail_password()
  16. receivers = MailConf().get_receivers()
  17. cc = MailConf().get_cc()
  18. # 用发件人的邮箱账号和授权码登陆服务器
  19. s = zmail.server(mail_username, mail_password, smtp_host=mail_host, smtp_port=mail_port)
  20. # 把html格式的测试结果报告文件的内容作为邮件正文进行发送
  21. with open(file_path, "r", encoding="utf-8") as f:
  22. mail_content = {"subject": "最新测试结果报告",
  23. "content_html": f.read(),
  24. "attachments": file_path}
  25. s.send_mail(receivers, mail_content, cc=cc)
  26. log.info(f"发送邮件成功,发送的测试结果报告文件是{file_path}")
  27. except Exception as e:
  28. log.error(f"发送邮件失败,原因是{str(e)}")
  29. if __name__ == '__main__':
  30. file_path = "..\\report\\测试结果报告.html"
  31. EmailUtil().send_email(file_path)