python使用SMTP发送邮件
2016-08-11 21:33
711 查看
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。通过SMTP协议所指定的服务器,就可以把E-mail寄到收信人的服务器上了,整个过程只要几分钟。SMTP服务器则是遵循SMTP协议的发送邮件服务器,用来发送或中转发出的电子邮件。
在自动化测试过程中,当测试执行完成之后,需要自己找到测试报告的位置,并且发送到目标邮箱中,以便查看最终的测试结果。
下面主要的内容是,将html的测试报告读取之后,放在邮件的内容中,并且把html测试报告放在附件中一起发送。
在自动化测试过程中,当测试执行完成之后,需要自己找到测试报告的位置,并且发送到目标邮箱中,以便查看最终的测试结果。
下面主要的内容是,将html的测试报告读取之后,放在邮件的内容中,并且把html测试报告放在附件中一起发送。
#coding:utf-8 import os,sys import smtplib import time from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart reportPath = os.path.join(os.getcwd(), 'report') # 测试报告的路径 # reportPath = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])),'report') class SendMail(object): def __init__(self,recver=None): """接收邮件的人:list or tuple""" if recver is None: self.sendTo = ['xxxxx.@qq.com'] #收件人这个参数,可以是list,或者tulp,以便发送给多人 else: self.sendTo = recver def get_report(self):# 该函数的作用是为了在测试报告的路径下找到最新的测试报告 dirs = os.listdir(reportPath) dirs.sort() newreportname = dirs[-1] print('The new report name: {0}'.format(newreportname)) return newreportname #返回的是测试报告的名字 def take_messages(self): # 该函数的目的是为了 准备发送邮件的的消息内容 newreport = self.get_report() self.msg = MIMEMultipart() self.msg['Subject'] = '测试报告主题' # 邮件的标题 self.msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') with open(os.path.join(reportPath,newreport), 'rb') as f: mailbody = f.read()# 读取测试报告的内容 html = MIMEText(mailbody,_subtype='html',_charset='utf-8') # 将测试报告的内容放在 邮件的正文当中 self.msg.attach(html) # 将html附加在msg里 # html附件 下面是将测试报告放在附件中发送 att1 = MIMEText(mailbody, 'base64', 'gb2312') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename="TestReport.html"'#这里的filename可以任意写,写什么名字,附件的名字就是什么 self.msg.attach(att1) def send(self): self.take_messages() self.msg['from'] = 'xxxxx@163.com' # 发送邮件的人 smtp = smtplib.SMTP('smtp.163.com',25) # 连接服务器 smtp.login('xxxxx@163.com','xxxxxxx') # 登录的用户名和密码 smtp.sendmail(self.msg['from'], self.sendTo,self.msg.as_string()) # 发送邮件 smtp.close() print('sendmail success') if __name__ == '__main__': sendMail = SendMail() sendMail.send() 最终的发送结果:
相关文章推荐
- python使用smtp发送邮件
- python3使用smtplib发送邮件
- python smtplib 使用代理发送邮件
- python - 如何通过GMail发送邮件:smtplib的使用
- python使用SMTP发送qq或sina邮件
- Python使用smtplib发送邮件
- python使用smtplib库和smtp.qq.com邮件服务器发送邮件
- python实战===使用smtp发送邮件的源代码,解决554错误码的问题
- Python 使用SMTP发送邮件的代码小结
- Python 使用第三方 SMTP 服务发送邮件(qq邮箱)
- 用python的smtplib发送邮件python2,3都可以使用
- python使用smtplib模块通过gmail实现邮件发送的方法
- Python使用SMTP发送邮件(163,yeah等网易邮箱已测试可以)
- Python使用SMTP自动发送邮件
- 使用python smtplib 发送带附件的邮件
- Python使用SMTP发送邮件
- python - 如何通过GMail发送邮件:smtplib的使用
- Python学习笔记(六) -- Python使用SMTP发送邮件
- python 使用smtp发送群邮件
- Python 使用SMTP发送邮件的代码小结