python exchangelib 如何获取特定日期范围内的邮件?
python exchangelib how to get email within specific date range?
我尝试用exchangelib提取群邮箱进行分析,想在一个日期范围内提取。
尝试使用过滤器功能,但似乎只适用于日历,请问您是否有电子邮件示例?
谢谢大家
您需要过滤消息项上可用的日期时间字段。 Message.FIELDS
包含 Message
class 上的所有可用字段。您可以列出所有日期时间字段,例如:
>>> [f.name for f in Message.FIELDS if f.value_cls == EWSDateTime]
['datetime_received', 'datetime_sent', 'datetime_created', 'reminder_due_by', 'last_modified_time']
README 显示了使用 .filter(start__range(x, y))
的示例,但 start
字段仅在 CalendarItem
对象上可用。相反,使用例如datetime_received
过滤 Message
个对象:
tz = EWSTimeZone.localzone()
emails_from_2017 = account.inbox.filter(datetime_received__range=(
tz.localize(EWSDateTime(2017, 1, 1)),
tz.localize(EWSDateTime(2018, 1, 1))
))
pytz_tz = pytz.timezone('Europe/Copenhagen') #setting the timezone
py_dt = pytz_tz.localize(datetime(year,month,day)) #building the custom date filter with a timezone object
ews_bfr = EWSDateTime.from_datetime(py_dt) #converting the custom date timezone object to a EWS (Exchange Web Service) date object
for item in account.inbox.all().order_by('-datetime_received')[:10000]: #look into the inbox the first 10K emails order desc by date received
if item.datetime_received < ews_bfr: #if the mail if older than the custom date in the EWS format then apply rule
item.delete() #delete all filtered emails
print("Mail deleted Successfully")
我尝试用exchangelib提取群邮箱进行分析,想在一个日期范围内提取。
尝试使用过滤器功能,但似乎只适用于日历,请问您是否有电子邮件示例?
谢谢大家
您需要过滤消息项上可用的日期时间字段。 Message.FIELDS
包含 Message
class 上的所有可用字段。您可以列出所有日期时间字段,例如:
>>> [f.name for f in Message.FIELDS if f.value_cls == EWSDateTime]
['datetime_received', 'datetime_sent', 'datetime_created', 'reminder_due_by', 'last_modified_time']
README 显示了使用 .filter(start__range(x, y))
的示例,但 start
字段仅在 CalendarItem
对象上可用。相反,使用例如datetime_received
过滤 Message
个对象:
tz = EWSTimeZone.localzone()
emails_from_2017 = account.inbox.filter(datetime_received__range=(
tz.localize(EWSDateTime(2017, 1, 1)),
tz.localize(EWSDateTime(2018, 1, 1))
))
pytz_tz = pytz.timezone('Europe/Copenhagen') #setting the timezone
py_dt = pytz_tz.localize(datetime(year,month,day)) #building the custom date filter with a timezone object
ews_bfr = EWSDateTime.from_datetime(py_dt) #converting the custom date timezone object to a EWS (Exchange Web Service) date object
for item in account.inbox.all().order_by('-datetime_received')[:10000]: #look into the inbox the first 10K emails order desc by date received
if item.datetime_received < ews_bfr: #if the mail if older than the custom date in the EWS format then apply rule
item.delete() #delete all filtered emails
print("Mail deleted Successfully")