Trying to get emails from the past 3 days from outlook, it almost works, but it's skipping emails, any idea why? [code inside, python] RRS feed

  • Question

  • Hope this is the right place. 

    So here's the code ... import win32com.client, datetime from datetime import timedelta

    def checkTime():
        date_filter_unformated = - timedelta(days=3)
        date_filter = date_filter_unformated.strftime("%m/%d/%y %I:%M:%S")
        current_message = all_messages.GetPrevious()
        message_time = current_message.ReceivedTime
        df_list = list(date_filter)
        mt_list = list(str(message_time))
        df_month, mt_month = int(''.join([df_list[0],df_list[1]])), int(''.join([mt_list[0],mt_list[1]]))
        df_day, mt_day = int(''.join([df_list[3],df_list[4]])), int(''.join([mt_list[3],mt_list[4]]))
        df_year, mt_year = int(''.join([df_list[6],df_list[7]])), int(''.join([mt_list[6],mt_list[7]]))
        if mt_year < df_year:
            return "Old"
        elif mt_year == df_year:
            if mt_month < df_month:
                return "Old"
            elif mt_month == df_month:
                if mt_day < df_day:
                    return "Old"
                    return "Pass"
            elif mt_month > df_month:
                return "Pass"
    def CurrentMessage(cm):
        print cm.Sender, cm.ReceivedTime
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                        # the inbox. Can change that number to reference
                                        # any other folder
    all_messages = inbox.Items
    current_message = all_messages.GetLast()
    while True:
        if checkTime() == "Pass":
        if checkTime() == "Old":

    So it should return the name of sender and date recieved of all emails within the past 3 days, but instead, it returns some of the emails from the 26th (today) and then all of the emails from the 23rd (3 days ago), with none from in between. I have no idea what's causing it to do this. Any suggestions?

    Tuesday, January 27, 2015 1:53 AM


All replies