1

I'm having the following problem to get the emails. Also, I want to filter emails by email_address domain.

Code:

from exchangelib import Q
from exchangelib import Account, Configuration, Credentials, DELEGATE, errors
...

account = connect(SERVER_URL, EMAIL, PASSWORD)

# Get the folder object
folder = account.root / 'Top of Information Store' / 'Inbox'

# Get emails
q = (Q(subject__contains='key_word') and Q(sender__contains='mydomain1.com'))
emails = folder.all().filter(q)

for item in emails:
  print(item.subject)

Output:

(Nothing Prints)
EEEEH
  • 759
  • 1
  • 7
  • 28

1 Answers1

0

__contains in combination with a list translates to "contains A and B". That's never going to match in your case. You need to use Q objects and boolean operators to get "contains A or B":

from exchangelib import Q

folder.filter(Q(sender__contains='mydomain1.com') | Q(sender__contains='mydomain2.com'))

See more at https://ecederstrand.github.io/exchangelib/#searching

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63