Search code examples
pythonoutlookexchangelib

Python exchangelib support multiple strings for filter path subject


I'm trying to search for emails with multiple subjects, but it only supports a single subject string in my code. I also want it to print if there is a missing subject in the search.

if not testfolder.filter(
    datetime_received__gt=since, 
    sender='anon@anon.dk',
    subject__icontains=['Backup - no index - Friday','backup - with no index']
    ).exists():
    print('Log mangler.. Sender Ticket til Kayako....')
else:
    print('email found')

ValueError: Value ['Backup - no index - Friday', 'backup - with no index'] for filter on field path "subject" must be a single value


Solution

  • You can use Q() objects and boolean logic for this. See https://ecederstrand.github.io/exchangelib/#searching

    E.g.:

    .filter(Q(subject__icontains='Backup - no index - Friday') | Q(subject__icontains='backup - with no index'))