import smtplib #SMTP是发送邮件的协议,smtplib是负责发送邮件
from email.mime.text import MIMEText
from email.header import Header
def send_email(test_report):
with open(test_report,'r',encoding='utf-8') as f:
mail_content = f.read()
sender = '*******.cn'
receiver = ['*****.com'] #可放多个邮箱地址
mail_server = 'smtp.sina.cn' #邮箱服务地址,可在邮箱设置里查找
email_name = '自动化测试报告'
username = "***********"
password = '***********'
message = MIMEText(mail_content,'html','utf-8')
message['Subject'] = Header(email_name,charset='utf-8')
message['From'] = Header(username)
#邮箱登录
smtp = smtplib.SMTP() #实例化邮箱
smtp.connect(mail_server)
smtp.login(username,password)
#发送邮件
for i in receiver:
smtp.sendmail(sender,i,message.as_string())
smtp.quit()
send_email()
- 注意:message[‘From’]的内容要与发件人保持一致,同时也不可少,否则会报错
- 错误信息:smtplib.SMTPDataError: (553, b’Envolope sender mismatch with header from…’)
来源:CSDN
作者:Unstoppable365
链接:https://blog.csdn.net/Unstoppable365/article/details/103888970