yaml文件结合zmail邮模块,做到代码和数据分离
前言
前几天自学了yaml文件,知道他的作用是做到代码和数据的分离,我需要发送测试报告的邮件给指定收件人,我如何处理呢??
代码框架图如下
yaml配置文件编写
如果不懂什么是yaml文件,如何编写,请点击查看什么是yaml,如何读取yaml文件
yaml文件读取
#! /usr/bin/python3
# -*- coding:utf-8 -*-
# @FileName: read_yaml.py
# @Time : 2020/8/19 16:21
# 公众号 : 码上开始
import yaml
def read_yaml():
# 定义yaml文件路径
yaml_path = "E:\\mail\\date\\yaml.yaml"
# 打开yaml文件
file = open(yaml_path, "r", encoding="utf-8")
# 读取
string = file.read()
dict = yaml.load(string)
print(dict)
return dict
发送邮件
#! /usr/bin/python3
# -*- coding:utf-8 -*-
# @FileName: mail.py
# @Time : 2020/8/19 16:20
# 公众号 : 码上开始
import zmail
# 导入common文件下的read_yaml函数
from common import read_yaml
# 调用yaml文件里的数据
response = read_yaml.read_yaml()
def send(send_mail, password, get_mail):
report_path = "E:\\mail\\report\\test.html"
MAIL = {
'subject': '邮件主题',
'content_text': '测试发送邮件',
'attachments': report_path,
}
server = zmail.server(send_mail, password)
# 如果有多个收件人,则用列表
server.send_mail(get_mail, MAIL)
send(response[0]["sendmail"], response[0]["password"], response[1]["getmail"])