python自动化email发送和添加附件
2017-12-29 14:23
323 查看
#-*- coding:utf-8 -*-
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import os
import time
# 发送邮箱
sender = "xxxxxxxx@163.com"
# 接收邮箱
receiver = "xxxxxxxxxxxx@qq.com"
# 发送邮件服务器
smtpserver = "smtp.163.com"
# 发送邮箱账号和密码
username = "xxxxxxxxxx@163.com"
password = "xxxxxxxxx"
# filename
filename = os.getcwd() +"/jd.html"
# 读取测试报告的内容
with open(filename, "rb") as f:
mail_body = f.read()
#定义邮件内容
msg = MIMEMultipart()
body = MIMEText(mail_body,_subtype='html', _charset='utf-8')
msg['Subject'] = u"自动化报告"
msg['from'] = sender
msg['to'] = receiver
#加上时间戳
msg["date"] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
msg.attach(body)
#添加附件
att = MIMEText(open(filename).read(), "base64", "gbk")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename= "jd.html"'
msg.attach(att)
#登录邮箱
smtp = smtplib.SMTP()
#连接邮箱服务器
smtp.connect(smtpserver)
#用户名密码
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print ('邮件已发送至对应账号!!')
相关文章推荐
- python 自动化:调取接口获取每日数据并写入xls,添加附件并发送email
- Python选择多个本地文件以附件发送到Email
- VC++ 利用MAPI实现在程序中调用默认的电子邮件程序发送EMAIL(可以添加附件)。
- Windows下python发送邮件_CustomEmail.py[text、html、附件、读取文本到正文]
- 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
- Python自动发送邮件并添加附件的主程序
- 发送Email并添加附件
- Python自动发送邮件并添加附件的主程序
- python 发送email邮件带附件
- iPhone App里添加能发送附件的Email功能
- Python发送多个附件和支持HTML及纯文本内容的 Email 实现
- Python自动发送邮件并添加附件的主程序
- python发送Email 带附件
- selenium2+python3自动化2-csdn写博客(邮件发送附件测试报告)
- [Python] 发送Email (可发HTML,附件)
- Python发送带附件的Email
- Python3.5 email发送邮件,包含txt、图片、HTML、附件
- 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
- Python自动发送邮件并添加附件的主程序
- python 使用smtplib发送带附件的email