python群发邮件,allure报告打包发送

更新时间:2023-06-18 16:10:11 阅读: 评论:0

python群发邮件,allure报告打包发送python发送邮件代码封装
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================
# @Time : 2020/10/26 20:32
# @File  : handle_email.py
# @Author: adeng
# @Date  : 2020/10/26
============================
"""
from datetime import datetime
import os
import yaml
import smtplib
from email.mime.multipart import MIMEMultipart
打台球规则from import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from scripts.handle_file import HandleFile
stants import ALLURE_REPORTS_DIR, EMAIL_CONF_FILE_PATH
class HandleEmail():
def __init__(lf, host="", port=25, *args, **kwargs):
lf.host = host
lf.port = port
# 添加⽂本
def add_text(lf, text):
return MIMEText(text, "plain", "utf-8")
# 添加html⽂本
def add_html_text(lf, html):
return MIMEText(html, "html", "utf-8")
# 添加附件,图⽚,txt,pdf,zip
def add_accessory(lf, filepath):
res = MIMEText(open(filepath, "rb").read(), "ba64", "utf-8")
res.add_header('Content-Disposition','attachment',filename=os.path.baname(filepath))
return res
# 添加主题发件⼈,收件⼈新手怎么下象棋
def add_subject_attach(lf, nd_email, receiver, subject, nder, attach_info:tuple, nd_date=None):
"""
@param nd_email: 发送⽅email
@param receiver:  接收⽅email群发是列表形式["**********",***********,....]
@param subject: 邮件主题
@param nder: 发送发的名字,⼀般指测试⼈员
@param attach_info: 构建附件元组
防诈骗黑板报
@param nd_date: 发送⽇期,"%Y-%m-%d %H:%M:%S",当为None时⽤当前时间发送邮件
@return: msg 可以传给 nd_email()⽅法发送邮件
"""
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
# msg['From'] = '{0} <{1}>'.format(datas["nd_email"][nd_email][0], nd_email)
msg['From'] = '{0} <{1}>'.format(nder, nd_email)
msg['To'] = ";".join(receiver)
if nd_date:
msg['Date'] = nd_date
el:
msg['Date'] = datetime.w(), "%Y-%m-%d %H:%M:%S")
if isinstance(attach_info, tuple):
for i in attach_info:
for i in attach_info:
msg.attach(i)
return msg
def nd_email(lf, nd_email, pwd, receiver, msg):
try:
smtp = smtplib.SMTP(lf.host, port=lf.port)
smtp.login(nd_email, pwd)
smtp.ndmail(nd_email, receiver, msg.as_string())
print("{0}给{1}发送邮件成功,发送时间:{2}".format(nd_email, receiver,
datetime.w(), "%Y-%m-%d %H:%M:%S")))
except Exception as e:
春色满园的意思print('SMTP Exception:\n' + str(e) + '\n')
rai e
finally:
smtp.quit()
def nd_allure_email(lf,nd_email,subject, report_name, nd_date=None, text="本邮件由系统⾃动发出,⽆需回复!\n各位同事,⼤家好,以下为本次测试报告."):        """
此⽅法是为公司定制发送Outputs/allure_reports的测试报告打包群发到邮件
数据读写的是configs/emailcof.yaml中的数据
@param nd_email: 发送⽅的邮件
@param subject:  主题格式字符串
@param report_name: 测试报告名字字符串
@param nd_date: ⽇期字符串 %Y-%m-%d %H:%M:%S
@param text: 正⽂⽂本
@return: None
alook插件"""
# ------加载conf/emailconf.yaml 数据----
with open(EMAIL_CONF_FILE_PATH, "r") as f:如何优雅的骂人
牡丹花期是几月份
datas = yaml.load(f, Loader=yaml.FullLoader)  # Loader=yaml.FullLoader 去除警告
print(datas)
try:
nder = datas["nd_email"][nd_email][0]
pwd = datas["nd_email"][nd_email][1]
receiver = datas["receiver"]
except Exception as err:
print("ERROR:\n"+str(err)+"\n")
print(f"请检查是否有将{nd_email}添加到emailconf配置⽂件⾥")
# 添加⽂本内容
text = text
text_plain = lf.add_text(text)
# 先将ALLURE_REPORTS_DIR打包成zip再发送附件
t = datetime.w(), "%Y%m%d%H%M%S_")
file_package_path = os.path.join(ALLURE_REPORTS_DIR, f"{t}{report_name}")
allure_report = HandleFile.make_package(file_package_path, "zip", ALLURE_REPORTS_DIR)
# 添加附件
乾隆几岁登基的
zip_allure = lf.add_accessory(allure_report)
# 构建附件元组
attach_info = (text_plain, zip_allure)
# 添加主题,附件信息添加到msg
msg = lf.add_subject_attach(nd_email, receiver, subject,nder, attach_info=attach_info,nd_date=nd_date)
# 发送邮件
lf.nd_email(nd_email, pwd, receiver, msg)
def nd_public_email(lf,nd_email, receiver, pwd, nder, subject, nd_date=None, text="",hmtl="",filepath=None):
attach_info = []
text_plain = lf.add_text(text=text)
attach_info.append(text_plain)
if hmtl:
text_html = lf.add_html_text(hmtl=hmtl)
attach_info.append(text_html)
elif filepath :
file_attach = lf.add_accessory(filepath=filepath)
attach_info.append(file_attach)
# 构建附件元组
attach_info = tuple(attach_info)
# 添加主题和附件信息到msg
msg = lf.add_subject_attach(nd_email,receiver,subject,nder,attach_info=attach_info,nd_date=nd_date)
# 发送邮件
lf.nd_email(nd_email, pwd, receiver, msg)

本文发布于:2023-06-18 16:10:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1044237.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:发送   邮件   添加
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图