Hamed
Hamed

Reputation: 574

fetching emails using exchangelib by message_id

I am maintaining an application using exchangelib. I need to be able to move emails to different folders, which is perfectly possible using exchangelib. However, I should give the possibility to my clients to do the same manually. What I want to be able to do is to see what email is moved to what folder later. So I need a tracking id for emails. So may questions are:

  1. Does the message_id field serve as this unique identifier?
  2. It seems it is not possible to fetch emails in bulk using message_id. What is the best practice for this? I can fetch emails using item_id and changekey, by calling account.fetch however, they change as the user moves email around different folders, while the message_id does not.

Upvotes: 2

Views: 3862

Answers (1)

Erik Cederstrand
Erik Cederstrand

Reputation: 10220

Re. 1, it seems you already found out that message_id is left untouched when an item is moved. I can't find any hard documentation from EWS regarding this fact, so make sure to do extensive testing.

Re. 2, it's true that you can only use account.fetch with item_id values. However, there are some things to note:

  1. When you call item.move(), item.item_id and item.changekey attributes are updated to the new location, which you could store for a future bulk operation. See https://github.com/ecederstrand/exchangelib/blob/3a1def29951d26c8a1b7021c7582c3d118181140/exchangelib/items.py#L365
  2. You can still fetch in bulk by message_id by first translating the message_id to an item_id. This does a FindItem call and then a GetItem call:

item_ids = account.inbox.filter(message_id__in=<your_message_ids>) \
    .values_list('item_id', 'changekey')
bulk_items = account.fetch(item_ids)

You probably want to chunk <your_message_ids> items so the __in filter does not get extremely large.

Upvotes: 2

Related Questions