发送带附件的邮件
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
from email.header import Header
from email.mime.application import MIMEApplication
import yaml
from Commom.Log import Mylogging
def mail():
"""发送带附件的邮件"""
my_sender = 'sender' # 发件邮箱
my_pass = 'pass' # 发件邮箱密码
my_user = 'user' # 收件箱
msg = MIMEMultipart() # 创建附件实例
msg['From'] = formataddr(['溺水的鱼', my_sender]) # 发件人昵称,邮箱
msg['To'] = formataddr(['wang-jun', my_user]) # 收件人昵称,邮箱
subject = '邮件附件测试'
msg['Subject'] = Header(subject, 'utf-8') # 邮件标题
msg.attach(MIMEText('接口自动化测试报告', 'plain', 'utf-8')) # 正文
# 构造附件,附件路径
report_zip = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '\测试报告.zip'
att = MIMEApplication(open(f'{report_zip}', 'rb').read())
att["Content-Type"] = "application/octet-stream"
att.add_header('Content-Disposition', 'attachment', filename='测试报告.zip')
msg.attach(att)
with smtplib.SMTP_SSL(host='smtp.qq.com', port=465) as server:
server.login(my_sender, my_pass)
server.sendmail(my_sender, [my_user, ], msg.as_string()) # 发送邮件
if __name__ == "__main__":
mail()