import zmail
from logzero import logger
def send_mails():
"""发送邮件"""
# 使用发送邮件的账号和密码:密码是授权码不是邮箱登陆密码
server = zmail.server("xxxx@163.com", "授权码")
# 发送邮件内容
# 读取html文件
with open(r"maoyan.html",
"r",
encoding="utf-8") as f:
content_html = f.read()
mail_content = {
"subject": "冒烟测试", # 邮件标题
"content_text": content_html, # 邮件内容,邮件正文
# "attachments": "userdata.xlsx", # 邮件附件 直接填写文件路径
}
# 发送邮箱
try:
# 发送多人并抄送多人
# server.send_mail(['foo@163.com','foo@126.com'],mail_content,cc=[('Boss','bar@163.com'),'bar@126.com'])
# 发送单人
server.send_mail("xxxxaa@163.com", mail_content)
logger.info("邮件发送成功")
except Exception as e:
logger.error(f"邮件发送失败: {e}")
def get_mail():
"""接收邮件"""
# 使用发送邮件的账号和密码:密码是授权码不是邮箱登陆密码
server = zmail.server("xxxo@163.com", "授权码")
# 收邮件
mail = server.get_latest()
if __name__ == '__main__':
send_mails()