发送1个附件
导入模块
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
登录邮箱
smtp=smtplib.SMTP('smtp.163.com',25)
smtp.login('邮箱账户', '邮箱密码')
设置邮件信息
sender='发件人姓名<发件人邮件地址'
receiver='收件人1姓名<收件人1邮件地址'
mailbody = '''
某经理:<p>
你好,第几轮接口自动化测试已经完成,测试报告参见附件,谢谢!
'''
设置邮件信息
mail=MIMEMultipart()
body=MIMEText(mailbody, 'plain', 'utf-8')
mail.attach(body)
mail['From']=formataddr(sender.split('<'))
mail['To']=formataddr(receiver.split('<'))
mail['Subject']='某项目自动化测试第几轮测试报告'
添加附件
attach1=MIMEText(open('../report/report.html','rb').read(), 'base64', 'utf-8')
.read()不能省略
Base64就是一种基于64个可打印字符来表示二进制数据的方法,Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,需要解码后才能阅读
attach1['Content-Type']='application/octet-stream'
octet:[ɒkˈtet]八位组,类似于bytes
attach1['Content-Disposition']='attachment; filename="test.html"'
disposition:安排方式
文件名自定义,但不能省略
mail.attach(attach1)
发送邮件
smtp.sendmail(sender, receiver, mail.as_string())
断开连接
smtp.quit()
使用qq邮箱发送发送邮件附件报告测试组长
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
smtp = smtplib.SMTP('smtp.qq.com', 25)
smtp.login('779146330@qq.com', 'idggecuopmudbbai')
sender = '暄总<779146330@qq.com'
receiver = '小暄总<sunbx@tedu.cn'
mailbody = '''
暄总:<p>
你好,第13轮接口自动化测试已经完成,测试报告参见附件,谢谢!
'''
mail = MIMEMultipart()
body = MIMEText(mailbody, 'plain', 'utf-8')
mail.attach(body)
mail['From'] = formataddr(sender.split('<'))
mail['To'] = formataddr(receiver.split('<'))
mail['Subject'] = '某项目自动化测试第几轮测试报告'
attach1 = MIMEText(open('./report/20210206_150830.html', 'rb').read(), 'base64', 'utf-8')
attach1['Content-Type'] = 'application/octet-stream'
attach1['Content-Disposition'] = 'attachment; filename="test.html"'
mail.attach(attach1)
smtp.sendmail(sender, receiver, mail.as_string())
smtp.quit()
发送多个附件
添加第2个附件
attach2=MIMEText(open('../log/log.txt','rb').read(), 'base64', 'utf-8')
attach2['Content-Type']='application/octet-stream'
attach2['Content-Disposition']='attachment; filename='log.txt'
mail.attach(attach2)
使用qq邮箱发送邮件并添加多个附件发给测试组长
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
smtp = smtplib.SMTP('smtp.qq.com', 25)
smtp.login('779146330@qq.com', 'idggecuopmudbbai')
sender = '暄总<779146330@qq.com'
receiver = '小暄总<sunbx@tedu.cn'
mailbody = '''
暄总:<p>
你好,第13轮接口自动化测试已经完成,测试报告参见附件,谢谢!
'''
mail = MIMEMultipart()
body = MIMEText(mailbody, 'plain', 'utf-8')
mail.attach(body)
mail['From'] = formataddr(sender.split('<'))
mail['To'] = formataddr(receiver.split('<'))
mail['Subject'] = '某项目自动化测试第几轮测试报告'
attach1 = MIMEText(open('./report/20210206_150830.html', 'rb').read(), 'base64', 'utf-8')
attach1['Content-Type'] = 'application/octet-stream'
attach1['Content-Disposition'] = 'attachment; filename="test.html"'
mail.attach(attach1)
attach2 = MIMEText(open('./log/20210206.log', 'rb').read(), 'base64', 'utf-8')
attach2['Content-Type'] = 'application/octet-stream'
attach2['Content-Disposition'] = "attachment; filename='log.txt'"
mail.attach(attach2)
smtp.sendmail(sender, receiver, mail.as_string())
smtp.quit()
作者:暄总-tester