接口自动化(六)--使用QQ邮箱发送邮件
接口测试执行完发送一个邮件,这里使用QQ邮箱发送,先要拿到QQ邮箱授权码,方法自行百度
1 # coding=utf-8 2 import smtplib 3 from email.mime.text import MIMEText 4 5 class sendEmail(): 6 def __init__(self,sender,password,resever,subject,content): 7 self.sender = sender 8 self.password = password #qq邮箱授权码 9 self.reserver = resever 10 self.subject = subject 11 self.content = content 12 13 def send(self): 14 message = MIMEText(self.content) 15 message["subject"] = self.subject 16 message["from"] = self.sender 17 message["to"] = self.reserver 18 try: 19 server = smtplib.SMTP_SSL("smtp.qq.com",465) 20 server.login(self.sender,self.password) 21 server.sendmail(self.sender,self.reserver,message.as_string()) 22 print("邮件发送成功") 23 except Exception as e: 24 print("邮件发送失败") 25 finally: 26 server.quit()
另外封装了一个模块,为了调用简单(直接RunSendMail(passlist,faillist).sendmail()就可以了)
1 from common import sendemail 2 3 class RunSendMail(): 4 def __init__(self,passlist,faillist): 5 self.passnum = passlist 6 self.failnum = faillist 7 self.sender = '#######@qq.com' 8 self.password = '*********' #QQ邮箱授权码 9 self.reserver = '#########@qq.com' 10 self.subject = "python邮件测试" 11 12 def config(self): 13 passnum = len(self.passnum) 14 failnum = len(self.failnum) 15 totalnum = passnum + failnum 16 passrate = "%2.f%%"%(passnum/totalnum*100) 17 failrate = "%2.f%%"%(failnum/totalnum*100) 18 content = "根据需要自己定义邮件内容" 19 return content 20 21 def sendmail(self):
22 sendemail.sendEmail(self.sender,self.password,self.reserver,self.subject,self.config()).send()