Python_定时自动发送邮件

来源:建筑界编辑:黄子俊发布时间:2020-03-22 19:39:32

[摘要] 虽然主流邮箱都可以定时发送邮件,但还是尝试用python写了一个定时发送邮件的脚本,设定好一个发送时间后,可以按下面步骤将文件通过QQ邮

虽然主流邮箱都可以定时发送邮件,但还是尝试用python写了一个定时发送邮件的脚本,设定好一个发送时间后,可以按下面步骤将文件通过QQ邮箱发送出去。

文件的最后修改及最后访问时间随机调整为发邮件前20~25分钟;
文件超过10M或是一个文件夹时,则自动生成zip压缩包;
通过邮箱自动发送。

有几个点需要特别注意一下:

修改文件的时间属性需要用到win32API的SetFileTime,所以需要先安装pywin32模块;
因为是通过SMTP服务发送的邮件,需要先在电子邮箱中打开SMTP服务
使用SMTP发送邮件需要邮箱地址和授权码,授权码需要在邮箱设置中获取

以下代码:

from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandlefrom win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTINGfrom pywintypes import Timefrom threading import Timerfrom email.header import Headerfrom email.mime.text import MIMETextfrom email.mime.application import MIMEApplicationfrom email.mime.multipart import MIMEMultipartimport time, os, random, zipfile, smtplib def ModifyFileTime(filePath, lastTime): #构造时间 fh = CreateFile(filePath, GENERIC_READ|GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) offsetSec=random.randint(1200, 1500) cTime, aTime, wTime = GetFileTime(fh) aTime = Time(time.mktime(lastTime) - offsetSec) wTime = Time(time.mktime(lastTime) - offsetSec) #修改时间属性 SetFileTime(fh, cTime, aTime, wTime) CloseHandle(fh) def GetFilePathList(filePath, result): if os.path.isdir(filePath): files = os.listdir(filePath) for file in files: if os.path.isdir(filePath + '/' +file): GetFilePathList(filePath + '/' + file, result) else: result.append(filePath + '/' + file) else: result.append(filePath) def RunZipFile(filePath): #构造压缩包路径 zipFilePath = os.path.basename(filePath) if os.path.isdir(filePath): zipFilePath = zipFilePath + '.zip' else: zipFilePath = os.path.splitext(zipFilePath)[0] + '.zip' #文件已存在则删除原有文件 if os.path.exists(zipFilePath): os.remove(zipFilePath) #创建压缩包 zipFileList = [] GetFilePathList(filePath, zipFileList) f = zipfile.ZipFile(zipFilePath, 'w', zipfile.ZIP_DEFLATED) for fileP in zipFileList: arcName = os.path.basename(fileP) if os.path.isdir(filePath): dirName = os.path.dirname(fileP) arcName = dirName.replace(os.path.dirname(dirName),'') + '/' + arcName f.write(fileP, arcName) f.close() return zipFilePath def SendEmail(filePath): #邮件设置 fromAddr = 'XXXXXXXXX@qq.com'#发件地址 autho = 'XXXXXXXXXXXXXXXX'#授权码 smtpServer = 'smtp.qq.com'#smtp服务器 #邮件信息 toAddr = 'XXXXXXXX@163.com'#收件地址 title = 'YourTitle'#主题 text = 'YourText'#正文 annexPath = filePath#附件路径 #构造邮件 message = MIMEMultipart() message['From'] = fromAddr message['To'] = toAddr message['Subject'] = Header(title, 'utf-8') message.attach(MIMEText(text, 'plain', 'utf-8')) #构造附件 annex = MIMEApplication(open(annexPath,'rb').read()) annex.add_header('Content-Disposition', 'attachment', filename = os.path.basename(annexPath)) message.attach(annex) #发送邮件 server = smtplib.SMTP_SSL(smtpServer, 465) server.login(fromAddr, autho) server.sendmail(fromAddr, [toAddr], message.as_string()) server.quit() def PreparingFilesAndSend(filePath, runTime): annexPath = filePath if os.path.isdir(filePath): #遍历文件,修改时间属性 modFileList = [] GetFilePathList(filePath, modFileList) for fileP in modFileList: ModifyFileTime(fileP, runTime) #打包 annexPath = RunZipFile(filePath) else: #修改时间属性 ModifyFileTime(filePath, runTime) #判断是否需要压缩 if os.path.getsize(filePath)/float(1024*1024) > 10: annexPath = RunZipFile(filePath) #发送邮件 SendEmail(annexPath) def TimedTask(filePath, runTime): interval = time.mktime(runTime) - time.mktime(time.localtime()) Timer(interval, PreparingFilesAndSend, args=(filePath, runTime)).start() fp = r"C:/Users/imfour/Documents/pythonTest/testtxt.txt"rt ="2019-6-17 02:08:00"#转化为结构化时间timeFormat ="%Y-%m-%d %H:%M:%S"s_time=time.strptime(rt, timeFormat) if __name__ =="__main__": TimedTask(fp, s_time)
发送邮件,定时,Python_

延展阅读

相关文章