python自动化email发送和添加附件

 
#-*- 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 ('邮件已发送至对应账号!!')
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值