python之发送HTML内容的邮件
2023-09-11 14:19:57 时间
1 # 发送html内容的邮件 2 import smtplib, time, os 3 from email.mime.text import MIMEText 4 from email.header import Header 5 6 7 def send_mail_html(file): 8 '''发送html内容邮件''' 9 # 发送邮箱 10 sender = 'zhangkai@192.168.20.190' 11 # 接收邮箱 12 receiver = 'gongxingrui@192.168.20.190' 13 # 发送邮件主题 14 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 15 subject = '自动化测试结果_' + t 16 # 发送邮箱服务器 17 smtpserver = '192.168.20.190' 18 # 发送邮箱用户/密码 19 username = 'zhangkai' 20 password = '123456' 21 22 # 读取html文件内容 23 f = open(file, 'rb') 24 mail_body = f.read() 25 f.close() 26 27 # 组装邮件内容和标题,中文需参数‘utf-8’,单字节字符不需要 28 msg = MIMEText(mail_body, _subtype='html', _charset='utf-8') 29 msg['Subject'] = Header(subject, 'utf-8') 30 msg['From'] = sender 31 msg['To'] = receiver 32 # 登录并发送邮件 33 try: 34 smtp = smtplib.SMTP() 35 smtp.connect(smtpserver) 36 smtp.login(username, password) 37 smtp.sendmail(sender, receiver, msg.as_string()) 38 except: 39 print("邮件发送失败!") 40 else: 41 print("邮件发送成功!") 42 finally: 43 smtp.quit() 44 45 46 def find_new_file(dir): 47 '''查找目录下最新的文件''' 48 file_lists = os.listdir(dir) 49 file_lists.sort(key=lambda fn: os.path.getmtime(dir + "\\" + fn) 50 if not os.path.isdir(dir + "\\" + fn) 51 else 0) 52 # print('最新的文件为: ' + file_lists[-1]) 53 file = os.path.join(dir, file_lists[-1]) 54 print('完整文件路径:', file) 55 return file 56 57 58 dir = 'D:\\test_data\\auto_test_result' # 指定文件目录 59 file = find_new_file(dir) # 查找最新的html文件 60 send_mail_html(file) # 发送html内容邮件
相关文章
- pandas 如何实现将数值按照前几位是否相同来聚合?(python)
- 报错 关于python的x和y不等长
- html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式
- 工作总结 @Html 辅助方法 为 生成的 标签设置元素属性 htmlAttributes 一个对象,其中包含要为该元素设置的 HTML 特性。
- Python 输出HTML实体字符(&#x***转html,html符号乱码,中文乱码)
- Word处理控件Aspose.Words功能演示:在 Python 中将 HTML 转换为 PNG、JPEG、BMP、GIF 或 TIFF 图像
- 零基础教你快速入门Python怎么学python入门?python新手学习路线
- Python初学者如何系统的学习python————Python入门学习指南--内附学习路径
- 2023最新版安装Python教程
- 139 python网络编程 - 网络概述
- Python中python-nmap模块的使用
- gyp ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- 【python】+704个常用工具Python库
- python - 面向对象(一)类,对象-类的定义和使用-self详解
- Python自动操作 GUI 神器——PyAutoGUI
- HTML-获取/修改html页面标题
- 一款很不错的html转xml工具-Html Agility Pack
- 构建字典时快捷设置默认值-python
- 《Python数据科学指南》——第1章 Python在数据科学中的应用 1.1 简介
- 《Python算法教程》——第2章 基础知识 2.1 计算领域中一些核心理念
- 哪种编程语言更适合编写Selenium Web驱动程序脚本,Python还是Java?
- python 爬虫之序列化html问题(类似u003)(去掉html中的标签比如<em>)
- 【Python】python扩展
- 小学生python游戏编程arcade----原码分析
- Python用python-docx读写word文档
- HTML---用记事本写html
- 【python百度智能云】:Python — 三种获取__VIEWSTATE、__VIEWSTATEGENERATOR、__EVENTVALIDATION方法。
- 【文件处理】——Python pandas 写入数据到excel中
- Python烟花秀