Search code examples
pythondatetimepython-datetimeexchangelib

How do I process email from timestamp with exchangelib?


I will be storing the last processed email timestamp to the database and then when I run the process I want to do greater than that email timestamp.

Right now I m doing this way.. which was fine to run for old records but now I want to automate this with a corn job run every 1 hour.


pytz_tz = pytz.timezone('US/Pacific')
py_dt = pytz_tz.localize(datetime(2022, 2, 3))
py_dt1 = pytz_tz.localize(datetime(2022, 2, 4))  ####ENTER DATE
ews_bfr = EWSDateTime.from_datetime(py_dt)
ews_bfr1 = EWSDateTime.from_datetime(py_dt1)
# print(ews_bfr)


query = Q(subject='MediaCore process')
recent_emails = account.inbox.filter(~query, datetime_received__range=(
    ews_bfr,
    ews_bfr1
))

But now instead of using the start and end date, I want to pass the timestamp of the email to start from, Can anyone please advise or help me with this?


Solution

  • I got my result with the help of dateutil.parse

    from dateutil.parser import parse
    
    timestampdate = "2022-02-07 18:16:45+00:00"
    recentkey = parse(timestampdate)
    
    recent_emails = account.inbox.filter(datetime_received__gt = recentkey) ##__gt and gte for greater than or greater than or equal to for filtering.