给多人发送邮件需要注意收件人传入的格式
def sendEmail():
emailHost = "smtp.163.com" # 获取smtp的host
sender ="lihua@163.com" # 获取发件人账号
# 如果配置文件读取收件人列表,需要转换格式,配置文件收件人列表不需要加引号和括号,如下配置
# recriver =lihua@163.com,lihua@163.com,lihua@163.com
# 读取后转换格式为list
recriver = ["lihua@163.com,lihua@163.com,lihua@163.com"] # 获取收件人账号,这个是列表类型
recrivers = ",".join(recriver)
password = "授权码" # 获取发件人账号对应的授权码
testReport = newReport() # 获取需要发送的报告
# 打开报告
with open(testReport, "r", encoding="utf-8") as f:
emailMessage = f.read()
# 发送HTML格式邮件,内容为报告内容
message = MIMEText(emailMessage, "html", "utf-8")
message["From"] = Header(sender) # 发件人
message["To"] = Header(recrivers) # 收件人
subject = "接口自动化测试报告" # 主题
message["Subject"] = Header(subject, "utf-8")
smtp = smtplib.SMTP_SSL(emailHost) # 实例化smtp
smtp.connect(host=emailHost, port=465) # 连接邮箱
smtp.login(sender, password) # 登录邮箱
smtp.sendmail(sender, recrivers.split",", message.as_string()) # 发送邮件
smtp.quit()
print("邮件发送成功")
if __name__ == '__main__':
sendEmail()