附上自己写的代码参考:
import os,time,smtplib
from email.header import Header
from email.mime.text import MIMEText
#定义发送邮件的设置
def sentemail(file_new):
#发送方
mail_from = 'xxxxxxx@163.com'
#接收方
mail_to = 'xxxxxx@qq.com'
#发送方用户名和密码
username = 'xxxxxxx@163.com'
password = '***********'
#发送文件标题
subject = '测试报告'
#打开文件
f= open(file_new,'rb')
#读取文件里面的内容
mail_body = f.read()
f.close()
#发送文件的格式
msg = MIMEText(mail_body,'html','utf-8')
#标题格式
msg['Subject'] = Header(subject, 'utf-8')
#来自于谁
msg['From'] = '谢'
#发送给谁
msg['To'] = 'XXXXXXX@qq.com'
#发送时间
msg['date'] = time.strftime('%a,%d %b %Y %H:%M:%S ,%z')
#发送邮箱的服务器
smtp = smtplib.SMTP()
#服务器的类型
smtp.connect('smtp.163.com')
#登录,要设置授权才可以
smtp.login(username, password)
smtp.sendmail(mail_from, mail_to, msg.as_string())
smtp.quit()
print("'email has send out")
#获取最新的报告
def sendrepost(result_dir):
#判断文件路径
lists = os.listdir(result_dir)
#根据系统时间获取最新的文件
lists.sort( key=lambda fn: os.path.getmtime(result_dir + '\\' + fn) if not os.path.isdir(result_dir + "\\" + fn) else 0)
print(u"最新文件为:" + lists[-1])
#最新文件的路径
file_new = os.path.join(result_dir, lists[-1])
print(file_new)
#调用发送最新邮件
sentemail(file_new)