'win43.client restrict combining many filters

I'm trying to read outlook emails via python, have code like below:

        import win32com.client
        import os 
        from datetime import datetime, timedelta
        
        outlook = win32com.client.Dispatch('outlook.application')
        mapi = outlook.GetNamespace('MAPI')
        
        messages = mapi.Folders("[email protected]").Folders("Inbox").Folders("Test").Items
        

       today_change = timedelta(7)
       today = datetime.today()
       start_date = today - today_change
       start_date = start_date.strftime('%Y-%m-%d %H:%M %p')
       today = today.strftime('%Y-%m-%d %H:%M %p')
        
        messages = messages.Restrict("[ReceivedTime] >= '" + start_date
        + "' And [ReceivedTime] <= '" + today +  "' And  @SQL=(urn:schemas:httpmail:subject LIKE '%deleted%')")

I'm trying to add more filters than 1, but I can't manage to do that. Can someone explain how should I add/merge more filters? Separately they work fine.



Solution 1:[1]

@SQL= prefix can only be used to prefix the whole query. Also, it is better to use PR_NORMALIZED_SUBJECT instead of PR_SUBJECT (the former property is what gets indexed).

Try the following (off the top of my head):

messages = messages.Restrict("@SQL=ReceivedTime >= '" + start_date
        + "' And ReceivedTime <= '" + today +  "' And ""http://schemas.microsoft.com/mapi/proptag/0x0E1D001F"" LIKE '%deleted%' ")

Solution 2:[2]

You need to use the SQL syntax in the following way:

messages = messages.Restrict("""@SQL="urn:schemas:httpmail:datereceived" >= '" + start_date
        + "' And "urn:schemas:httpmail:datereceived" <= '" + today +  "' And "urn:schemas:httpmail:subject" LIKE '%deleted%')""")

See Filtering Items Using Query Keywords for more information.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2