A sort command is available, but it is not guaranteed to support IMAP server. For example, Gmail does not support the SORT command.
To try the sort command, you must replace:
M.search(None, 'ALL')
from
M.sort(search_critera, 'UTF-8', 'ALL')
Then search_criteria will be a string like:
search_criteria = 'DATE' #Ascending, most recent email last search_criteria = 'REVERSE DATE' #Descending, most recent email first search_criteria = '[REVERSE] sort-key' #format for sorting
According to RFC5256 , they are valid sort-key :
"ARRIVAL" / "CC" / "DATE" / "FROM" / "SIZE" / "SUBJECT" / "TO"
Notes:
1. requires encoding, try US-ASCII or UTF-8 , all the rest should not be supported by the IMAP server
2. A search criteria is also required. The ALL command is valid, but there are many. See http://www.networksorcery.com/enp/rfc/rfc3501.txt for more details.
The world of IMAP is wild and crazy. Good luck.
rfadams
source share