When you first select a folder, the IMAP server provides some summary information about it—about the
folder itself and also about its messages.
The summary is returned by IMAPClient
as a dictionary. Here are the keys that most IMAP servers
will return when you run select_folder()
:
EXISTS: An integer giving the number of messages in the folder.
FLAGS: A list of the flags that can be set on messages in this folder.
select_folder()
on
it.Of these flags, servers are only required to return FLAGS, EXISTS, and RECENT, though most will include at least UIDVALIDITY as well.
folder_info.py
shows an example program that reads and displays the
summary information of my INBOX mail folder:
import getpass, sys
from imapclient import IMAPClient
try:
hostname, username = sys.argv[1:]
except ValueError:
print 'usage: %s hostname username' % sys.argv[0]
sys.exit(2)
c = IMAPClient(hostname, ssl=True)
try:
c.login(username, getpass.getpass())
except c.Error, e:
print 'Could not log in:', e
sys.exit(1)
else:
select_dict = c.select_folder('INBOX', readonly=True)
for k, v in select_dict.items():
print '%s: %r' % (k, v)
c.logout()
When run, this program displays results such as this:
```
root@erlerobot:~/Python_files# python folder_info.py imap.example.com [email protected] Password: EXISTS: 3 PERMANENTFLAGS: ('\Answered', '\Flagged', '\Draft', '\Deleted', '\Seen', '\*') READ-WRITE: True UIDNEXT: 2626 FLAGS: ('\Answered', '\Flagged', '\Draft', '\Deleted', '\Seen') UIDVALIDITY: 1 RECENT: 0
```
That shows that my INBOX folder contains three messages, none of which have arrived since I last checked. If your program is interested in using UIDs that it stored during previous sessions, remember to compare the UIDVALIDITY to a stored value from a previous session.