import win32com.client
import os
import re
import datetime
邮件存在的目录,至少支持4级目录
folder = “liaoqy@uvasset.com>收件箱>NetGain报表”
获取当天日期字符串
today_str = (datetime.date.today()).strftime(“%Y-%m-%d”)
today_file = (datetime.date.today()).strftime(“%Y%m%d”)
start_day = (datetime.date.today() - datetime.timedelta(days=8)).strftime(“%Y-%m-%d”)
end_day = (datetime.date.today() - datetime.timedelta(days=2)).strftime(“%Y-%m-%d”)
folder_path = “Y:\SysAdmin\逻辑配置\B网络\B10_Netgain监控系统\报表\” + today_file
folder_path1 = “Y:\SysAdmin\逻辑配置\B网络\B09_防垃圾邮件系统\报表\” + today_file
if not os.path.exists(folder_path):
os.makedirs(folder_path)
if not os.path.exists(folder_path1):
os.makedirs(folder_path1)
打开Outlook Express应用程序
outlook = win32com.client.Dispatch(“Outlook.Application”)
namespace = outlook.GetNamespace(“MAPI”)
inbox = ‘’
folder_list = folder.split(“>”)
for i in folder_list:
for f_folder in namespace.Folders:
if f_folder.Name == i:
namespace = f_folder
inbox = f_folder
break
if inbox:
# 获取当天所有邮件
messages = inbox.Items.Restrict(“[ReceivedTime] >= '” + today_str + " 00:00 AM’“)
# 遍历所有邮件
i = 0
for message in messages:
# 判断是否包含特定主题关键字
if “报表发送-上周” in message.Subject:
# 获取附件列表
attachments = message.Attachments
# 遍历附件列表并保存到本地文件夹
i += 1
for attachment in attachments:
title = message.Subject
title = re.sub(r”\s+“, “”, title)
title = re.findall(r"上周-(.+)(每周一)”, title)
title = start_day + ‘~’ + end_day + ’ ’ + title[0]
print(title)
attachment.SaveAsFile(
os.path.join(r"Y:\SysAdmin\逻辑配置\B网络\B10_Netgain监控系统\报表\" + today_file, title + ‘.pdf’))
if “SPAM SQR统计报表发送” in message.Subject:
# 获取附件列表
attachments = message.Attachments
# 遍历附件列表并保存到本地文件夹
for attachment in attachments:
print(attachment.FileName)
attachment.SaveAsFile(
os.path.join(r"Y:\SysAdmin\逻辑配置\B网络\B09_防垃圾邮件系统\报表\" + today_file, attachment.FileName))