标签:访问 str 简单的 name mamicode 查看 绝对路径 上传 san
SMTP
(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP
访问163邮箱
举例子
客户端授权码
,记下来就可以了,后面会用到1 [EMAIL] 2 on_off = on 3 # 邮件主题 4 subject = 接口自动化测试报告 5 #发送邮件信息 6 mail_host = smtp.163.com 7 mail_user = 发送人邮箱@163.com 8 # 163邮箱授权密码 9 mail_pass = 密码或授权密码 10 # 发送人 11 sender = 发送人@163.com 12 # 接收人 13 receivers = 接收人@qq.com
1 import os 2 import configparser 3 import getPathInfo 4 5 path = getPathInfo.get_Path() # 调用实例化 6 config_path = os.path.join(path, ‘config.ini‘) # 在path路径下再加一级,即绝对路径\config.ini 7 config = configparser.ConfigParser() # 调用外部的读取配置文件的方法,实例化config 8 config.read(config_path, encoding=‘utf-8‘) 9 10 11 class ReadConfig(): 12 def get_http(self, name): 13 value = config.get(‘HTTP‘, name) 14 return value 15 16 def get_email(self, name): 17 value = config.get(‘EMAIL‘, name) 18 return value 19 20 def get_mysql(self, name): 21 value = config.get(‘DATABASE‘, name) 22 return value
1 import os 2 3 4 def get_Path(): 5 path = os.path.split(os.path.realpath(__file__))[0] 6 return path
1 import os 2 import smtplib 3 import time 4 import readConfig 5 import getPathInfo 6 from email.mime.text import MIMEText # 发送正文 7 from email.mime.multipart import MIMEMultipart # 发送多个部分 8 from email.mime.application import MIMEApplication # 发送附件 9 from email.header import Header # 从email包引入Header()方法,是用来构建邮件头 10 11 read_conf = readConfig.ReadConfig() 12 mail_host = read_conf.get_email(‘mail_host‘) # 从配置文件中读取,邮件host 13 mail_user = read_conf.get_email(‘mail_user‘) # 从配置文件中读取,登录邮箱用户名 14 mail_pass = read_conf.get_email(‘mail_pass‘) # 从配置文件中读取,登录邮箱密码 15 subject = read_conf.get_email(‘subject‘) # 从配置文件中读取,邮件主题 16 sender = read_conf.get_email(‘sender‘) # 从配置文件中读取,邮件发送人 17 receivers = read_conf.get_email(‘receivers‘) # 从配置文件中读取,邮件收件人 18 mail_path = os.path.join(getPathInfo.get_Path(), ‘result‘, ‘report.html‘) # 获取测试报告路径 19 20 21 class TestMail(object): 22 23 def send_mail(self): 24 # 构造一个邮件体:正文、附件 25 msg = MIMEMultipart() # 邮件体 26 msg[‘From‘] = sender # 发件人 27 msg[‘To‘] = receivers # 收件人 28 tm = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) # 获取系统时间 29 msg[‘Subject‘] = Header(subject + ‘_‘ + tm, ‘utf-8‘) # 邮件主题 30 # 构建正文 31 content = """ 32 执行测试中…… 33 测试已完成!! 34 生成报告中…… 35 报告已生成…… 36 报告已邮件发送!! 37 """ 38 email_body = MIMEText(content, ‘plain‘, ‘utf-8‘) 39 msg.attach(email_body) # 将正文添加到邮件体中 40 # 构建附件 41 att = MIMEApplication(open(mail_path, ‘rb‘).read()) # 打开附件 42 att.add_header("Content-Disposition", "attachment", filename=‘测试报告.html‘) # 为附件命名 43 msg.attach(att) # 添加附件到邮件体中 44 45 try: 46 smtpObj = smtplib.SMTP() 47 smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 使用smtp协议发送邮件,SSL协议来进行加密传输,465为端口号 48 smtpObj.login(mail_user, mail_pass) # 邮箱登录 49 smtpObj.sendmail(sender, receivers, msg.as_string()) # 发送邮件 50 print(‘send mail ok‘) 51 smtpObj.quit() 52 except smtplib.SMTPException: 53 print(‘send mail fail‘) 54 55 56 if __name__ == "__main__": 57 send = TestMail() 58 send.send_mail()
优化:可加上定时发送
借鉴博客:
标签:访问 str 简单的 name mamicode 查看 绝对路径 上传 san
原文地址:https://www.cnblogs.com/leo-yu/p/12674606.html