Imap library has a search method. There is almost ready-to-use code for you.
#!/usr/bin/env python import imaplib from re import findall MAXSIZE = 1000 MINSIZE = 1 m = imaplib.IMAP4_SSL('imap.gmail.com') m.login(' example@gmail.com ','testPassword') m.select() typ, data = m.search(None, 'ALL') typ, data = m.search(None,'(SMALLER %d) (LARGER %d)' % (MAXSIZE * 1000,MINSIZE * 1000)) for num in data[0].split(): typ, data = m.fetch(num, '(RFC822)') print 'Message %s\n%s\n' % (num, len(data[0][1])) m.close() m.logout()
source share