python: imaplib - no way to sort feched uids by date - office365's fault

One could instinctively think that when you search your emails using imaplib as per below, and you get a list of all the uids of the emails that john doe has sent, that there's a correlation between the UID and the date.

result, numbers = mail.uid('search', 'FROM', 'john.doe@example.com', '(SINCE "01-Jan-2020")') for example, if you get all the uids from this person: [11111,222222,333333,444444,555555] you would think that the 555555 is the uid of the latest email. If you thought that, as myself you are wrong. imaplib has a sort option which apparently lets you sort the uids by date (which is what I wanted), I basically wanted the latest email from each recipient, but that sort option (or extension as its how they call it) is not supported by office 365, which is my company's email service provider. I had to do a complicated solution which was to download all the email headers and use the python code to sort and select the latest email. This is time and bandwidth consuming.

Leave a Reply

Your email address will not be published. Required fields are marked *