Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python exchangelib how to get email within specific date range?

I tried to use exchangelib to extract group mailbox for analysis, and i want to extract within a date range.
tried to use Filter function but seems only work for calender, may I have your advise is there any sample for email ? thanks all.

like image 650
user2672033 Avatar asked Feb 12 '18 08:02

user2672033


1 Answers

You need to filter on a datetime field that is available on Message items. Message.FIELDS contains all available fields on the Message class. You can list all datetime fields with something like:

>>> [f.name for f in Message.FIELDS if f.value_cls == EWSDateTime]
['datetime_received', 'datetime_sent', 'datetime_created', 'reminder_due_by', 'last_modified_time']

The README shows examples using .filter(start__range(x, y)), but the start field is only available on CalendarItem objects. Instead, use e.g. datetime_received to filter Message objects:

tz = EWSTimeZone.localzone()
emails_from_2017 = account.inbox.filter(datetime_received__range=(
    tz.localize(EWSDateTime(2017, 1, 1)),
    tz.localize(EWSDateTime(2018, 1, 1))
))
like image 62
Erik Cederstrand Avatar answered Oct 14 '22 02:10

Erik Cederstrand