Python实现接口自动化框架(简易)实践二 如何将测试报告以邮件形式发送给别人呢,这就是这次博文的内容啦 Python库对于邮件的发送主要有两个模块:smtplib模块和email模块 1、smtplib模块 主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容) 基本语法: import smtplib smtp = smtplib.SMTP() smtp.connect('smtp.163.com,25') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() 说明: smtplib.SMTP():实例化SMTP() connect(host,port): host:指定连接的邮箱服务器。常用邮箱的smtp服务器地址如下: 新浪邮箱:smtp.sina.com,新浪VIP:smtp.vip.sina.com,搜狐邮箱:smtp.sohu.com,126邮箱:smtp.126.com,139邮箱:smtp.,163 网易邮箱:smtp.163.com。 port:指定连接服务器的端口号,默认为25. login(user,password): user:登录邮箱的用户名。 password:登录邮箱的密码,像笔者用的是网易邮箱,网易邮箱一般是网页版,需要用到客户端密码,需要在网页版的网易邮箱中设置授权 码,该授权码即为客户端密码。 sendmail(from_addr,to_addrs,msg,...): from_addr:邮件发送者地址 to_addrs:邮件接收者地址。字符串列表['接收地址1','接收地址2','接收地址3',...]或'接收地址' msg:发送消息:邮件内容。一般是msg.as_string():as_string()是将msg(MIMEText对象或者MIMEMultipart对象)变为str。 quit():用于结束SMTP会话。 2、email模块 主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。 主要有三个模块: from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage 分别用于整合发送,发送文档和html或附件等,发送图片 主要语法: msg = MIMEMultipart('mixed') msg['Subject'] = 'Python email test' msg['From'] = 'XXX@163.com <XXX@163.com>' msg['To'] = 'XXX@126.com' msg['Date']='2012-3-16' msg.attach(text_plain) msg.attach(text_html) msg.attach(text_att) msg.attach(image) 下面是我封装好的方法,可直接调用: def send_(self, msg): """ 设置邮箱服务器,登录邮箱 定义发送者和接收者,并发送邮件 """ msg['Subject'] = Header("自动化测试报告", 'utf-8') sender = "XXXXXXXX@163.com" reciver = ["MMMMMMM@163.com", "NNNNNNNNN@qq.com"] msg['From'] = "XXXXXXXX@163.com" msg['To'] = ";".join(reciver) smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login("XXXXXXXX@163.com", "******") smtp.sendmail(sender, reciver, msg.as_string()) smtp.quit() print("邮件已发出!注意查收!") def send_mail_by_html(self): """ 发送html文本 """ f = open(self.new_report(), 'rb') mail_body = f.read() f.close() msg = MIMEText(mail_body, 'html', 'utf-8') self.send_(msg) def send_mail_by_enclosure(self): """ 发送附件 """ f = open(self.new_report(), 'rb') mail_body = f.read() f.close() msg = MIMEText(mail_body, 'base64', 'utf-8') msg["Content-Type"] = 'application/octet-stream' msg.add_header('Content-Disposition', 'attachment', filename="test_report.html") self.send_(msg) def send_mail_by_body(self): """ 发送正文 """ mail_body = u"测试报告,请注意查收!" msg = MIMEText(mail_body, 'plain', 'utf-8') self.send_(msg) def send_mail_by_hypertext(self): """ 发送超级连接 """ mail_body = """ <html> <body> <p> Here is the <a href="http://www.baidu.com">link</a> you wanted. </p> </body> </html> """ msg = MIMEText(mail_body, 'html', 'utf-8') self.send_(msg) def send_mail_by_image(self): """ 发送图片 """ mail_body = open(r'D:\picture\desk.png', 'rb').read() image = MIMEImage(mail_body) image.add_header('Content-ID', '<image1>') self.send_(image) def send_mail_by_all(self): """ 发送正文+附件 """ msg = MIMEMultipart('mixed') msg['Subject'] = Header("自动化测试报告", 'utf-8') sender = "XXXXXXXX@163.com" reciver = ["mmmmmmm@qq.com", "nnnnnnnnnn@qq.com"] msg['From'] = "XXXXXXXX@163.com" msg['To'] = ";".join(reciver) s = open(self.new_report(), 'rb') mail_body = s.read() s.close() msg_2 = MIMEText(mail_body, 'html', 'utf-8') msg.attach(msg_2) f = open(self.new_report(), 'rb') mail_body = f.read() f.close() msg_1 = MIMEText(mail_body, 'base64', 'utf-8') msg_1["Content-Type"] = 'application/octet-stream' msg_1.add_header('Content-Disposition', 'attachment', filename="test_report.html") msg.attach(msg_1) smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login("XXXXXXXX@163.com", "******") smtp.sendmail(sender, reciver, msg.as_string()) smtp.quit() print("邮件已发出!注意查收!") 这样很方便得就可将测试报告等信息通过邮件发送给相关人员,大大减少了沟通时间,增加了工作效率!!同时和可以进行日志或者网页的监控,将监控信息发送给相关人员!
|