实现自动发邮件功能

在实际的项目中,当脚本执行完毕,生成测试报告,咱们须要把报告经过邮件发送到对应的测试人员和开发人员,下面学习下在Python中如何实现邮件发送功能,在Python中提供了smtplib模块来发送邮件,导入smtplib,经过help函数能够查看smtp提供的方法。html

在学习发邮件的过程当中,只须要掌握两个模块的用法便可,smtplib和email,smtplib模块负责发送邮件(链接邮箱服务器,登陆邮箱,发送邮件),email负责构造邮件(发件人,收件人,主题,正文,附件等)。服务器

smtplib模块:app

 1 connect(host,port):  2 host:指定链接的邮箱服务器  3 port:指定链接服务器的端口号,默认为25  4 login(user,password):  5 usr:指定邮箱用户名  6 password:指定邮箱登陆密码  7 sendmail(from_addr, to_addrs, msg):  8 from_addr:指定邮件发送者地址  9 to_addrs:指定邮箱接受者地址 10 msg:发送消息:邮件内容,通常是msg.as_string()将msg(MIMEText对象或者MIMEMultipart对象)变为str。 11 quit():
用来结束SMTP回话。

email模块:函数

email模块下有mime包,该包下经常使用的三个模块是:text,image,multipart。学习

构造一个MIMEText对象,就表示一个文本邮件对象,构造一个MIMEImage对象,就表示一个做为附件的图片,若是要把多个对象组合到一块儿,则须要使用MIMEMultipart对象。测试

发送普通的文本:text_plain=MIMEText(text,"plain","utf-8")ui

发送HTML格式的文本:text_html=MIMEText(text,"html","utf-8")spa

在进行发送邮件时,咱们须要把subject,from,to等添加到MIMEText或者MIMEMultipart对象中,由于只有这样,邮件才会显示主题,发件人,收件人等。code

msg = MIMEMultipart() msg["subject"] = "这是发送邮件的主题" msg["from"] = sender msg["to"] = recever

说明:若是只有一个html网页或者plain普通文本的话,msg能够是MIMEText,可是若是是多个的话(附件,文本,图片等),则msg类型要为MIMEMultipart。server

1.发送HTML格式的邮件

# -*-coding:utf-8 -*- import smtplib from email.mime.text import MIMEText # ------1、设置发送邮件相关的参数------ smtpserver ="smtp.126.com" # 发送邮件服务器 sender ="xxxxxx" # 发送邮件的帐号 pwd = "xxxxxx" # 发送邮件帐号的密码 receiver = "xxxxxx" # 接收邮件的帐号 # ------2、编辑邮件的内容----- subject = "发送邮件的主题" # 发送邮件的主题 body = "这是发送的邮件的内容" # 发送邮件的内容,邮件的正文为html格式 msg = MIMEText(body, "html", "utf-8") # 发送纯文本格式的邮件 msg["Subject"] = subject msg['from'] = "xxxxxx" msg['to'] = "xxxxxx" # ------3、发送邮件------ smtp = smtplib.SMTP() smtp.connect(smtpserver) # 链接邮件服务器 smtp.login(sender, pwd) # 登陆邮件服务器 smtp.sendmail(sender, receiver, msg.as_string()) # 发送邮件 smtp.quit() # 关闭链接

2.发送带附件的邮件

上面的MIMEText只能发送正文格式的邮件,没法发送附件,若是想要发送带附件的邮件,须要另一个类MIMEMultipart。

# -*-coding:utf-8 -*- import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # ------1、设置发送邮件相关的参数------ smtpserver ="smtp.126.com" # 发送邮件服务器 sender ="xxxxxx" # 发送邮件的帐号 pwd = "xxxxxx" # 发送邮件帐号的密码 receiver = "xxxxxx" # 接收邮件的帐号 # ------2、编辑邮件的内容----- # 读文件 file_path = "./testpro/testresult/2019-03-10 09-35-54result.html" with open(file_path, "rb") as fp: mail_body = fp.read() msg = MIMEMultipart() msg['from'] = sender msg['to'] = receiver msg["subject"] = "这是发送邮件的主题" # 正文 body = MIMEText(mail_body, "html", "utf-8") msg.attach(body) # 附件 attach = MIMEText(mail_body, "base64", "utf-8") attach["Content-Type"] = "application/octet-stream" attach["Content-Disposition"] = 'attachment; filename="testReport.html"' msg.attach(attach) # ------3、发送邮件------ smtp = smtplib.SMTP() smtp.connect(smtpserver) # 链接邮件服务器 smtp.login(sender, pwd) # 登陆邮件服务器 smtp.sendmail(sender, receiver, msg.as_string()) # 发送邮件 smtp.quit() # 关闭链接

 3.邮件发送最新的测试报告

# -*-coding:utf-8 -*- import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import os from email.header import Header # ------1、设置发送邮件相关的参数------ smtpserver ="smtp.126.com" # 发送邮件服务器 sender ="xxxxxx" # 发送邮件的帐号 pwd = "xxxxxx" # 发送邮件帐号的密码 receiver = "xxxxxx" # 接收邮件的帐号 # ------2、编辑邮件的内容----- result_dir = "./testpro/testresult/" file_list = os.listdir(result_dir) file_list.sort(key=lambda fn: os.path.getmtime(result_dir + "/" + fn)) file = os.path.join(result_dir+file_list[-1]) # 读文件 with open(file, "rb") as fp: mail_body = fp.read() msg = MIMEMultipart() msg["subject"] = "这是发送邮件的主题" msg["from"] = sender msg["to"] = receiver # 正文 body = MIMEText(mail_body, "html", "utf-8") msg.attach(body) # 附件 attach = MIMEText(mail_body, "base64", "utf-8") attach["Content-Type"] = "application/octet-stream" attach["Content-Disposition"] = 'attachment; filename="testReport.html"' msg.attach(attach) # ------3、发送邮件------ smtp = smtplib.SMTP() smtp.connect(smtpserver) # 链接邮件服务器 smtp.login(sender, pwd) # 登陆邮件服务器 smtp.sendmail(sender, receiver, msg.as_string()) # 发送邮件 smtp.quit() # 关闭链接

 4.整合自动发送邮件功能

下面根据上节的测试百度搜索和搜狗搜索的案例整合自动发送邮件功能。

# coding:utf-8 import unittest from HTMLTestRunner import HTMLTestRunner import time from smtplib import SMTP from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import os def send_mail(report): # 设置发送邮件须要的参数 smtp_server = "smtp.126.com" sender = "xxxxxx" pwd = "xxxxxx" recever = "xxxxxx" # 设置发送邮件的内容 # 读文件 with open(report, "rb") as fp: mail_body = fp.read() msg = MIMEMultipart() msg["subject"] = "这是发送邮件的主题" msg["from"] = sender msg["to"] = recever # 内容 body = MIMEText(mail_body,"html","utf-8") msg.attach(body) # 附件 attach = MIMEText(mail_body, "base64", "utf-8") attach["Content-Type"] = "application/octet-stream" attach["Content-Disposition"] = 'attachment; filename="testReport.html"' msg.attach(attach) # 发送邮件 smtp = SMTP() smtp.connect(smtp_server) smtp.login(sender, pwd) smtp.sendmail(sender, recever, msg.as_string()) smtp.quit() def find_report(file_path): list_file = os.listdir(file_path) # 列出该目录下的全部文件 list_file.sort(key=lambda fn: os.path.getmtime(file_path + "/" + fn)) report = os.path.join(file_path + list_file[-1]) return report if __name__ == '__main__': discover = unittest.defaultTestLoader.discover("./testpro/testcase/", "test*.py") now_time = time.strftime("%Y-%m-%d %H-%M-%S") file_name = "./testpro/testresult/"+now_time+"result.html" file = open(file_name, "wb") runner = HTMLTestRunner(stream=file, title="测试报告", description="测试用例执行状况") runner.run(discover) file.close() new_report = find_report("./testpro/testresult/") send_mail(new_report) print("The end!")
相关文章
相关标签/搜索