A
A
Andrey2020-08-15 11:41:08
Python
Andrey, 2020-08-15 11:41:08

How to get all the items in the Inbox folder and all its subfolders in Exchangelib?

How can I search for messages in the inbox and all subfolders without going through all the folders? The option below with reading all the folders separately in turn is not suitable.

my_folder = account.inbox / 'myfolder'
for i in my_folder.all():
    print(i.subject)


The option below with iterating over folders does not work:
from exchangelib.folders import Messages

for f in account.folders[Message]:
    for i in f.filter(datetime_received__gt=emails_since):
        print(i)


With this approach he writes:
File "test.py", line 27, in getitem
for f in self.account.folders[Messages]:
AttributeError: 'Account' object has no attribute 'folders'


I think the matter is in the antiquity of this decision.

UPDATE:

Got out with this search:
def getitem(self,c):  # Вернет количество последних писем равное c из подпапок входящие
        self.my_folder = self.account.root / 'Корневой уровень хранилища'
        for f in self.my_folder.walk().filter(subject__contains='CallKeeper').order_by('-datetime_received')[:c]:
            # for i in f.filter(subject__contains='a'):
            print(f.subject, f.datetime_received)


But since inside the walk () function is also organized by enumeration, the search result gives the first one that comes across, i.e.: if it finds the required number of messages in the inbox, it sends the result without looking through all the other folders, but I need to the search was for the most recent message with a subject containing "CallKeeper" in the inbox and all its attachments. Tried through:
FolderCollection(account=self.account, folders=[self.account.inbox, self.account.inbox / 'CallKeeper', self.account.inbox / 'folder2>']).filter(subject__contains='CallKeeper').order_by ('-datetime_received')[:c]:

the result is the same.
Maybe someone has ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PavelMos, 2020-08-15
@PavelMos

See manually what is contained in the object that should be looped over, whether it even has
dir(account)
print(account)
and
print(account.__doc__)
print(account.folder.__doc__)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question