Understand Message Identifiers (UIDs vs Sequence Numbers)
masterIMAP messages are identified in two ways:
- Message Sequence Numbers: Integers from 1 to N (the total number of messages in a folder). These are volatile and can change if messages are deleted or expunged.
- Unique Identifiers (UIDs): Integers assigned by the server that persist across sessions and remain stable even after expunging.
Configuration:
IMAPClientuses UIDs by default.- You can control this behavior using the
use_uidargument during instantiation or by modifying theuse_uidattribute on an existing instance.
Usage Patterns: Methods accepting message IDs can take:
- A single integer:
123 - A sequence of integers:
[1, 2, 3] - A string representing IMAP ranges/sets:
'50-65','2:*', or'2,4:7,9,12:*'
# Using UIDs (default)
client = IMAPClient('imap.example.com', use_uid=True)
# Switching to sequence numbers for a specific operation
client.use_uid = False
client.fetch('[1:5]', ['BODY[]'])
# Using range strings
client.add('/Seen', '1:10')