search mail by date with imaplib

rweth rweth at cisco.com
Sat Jan 6 14:49:17 EST 2007


csselo at gmail.com wrote:
> Hi
> I am looking for a code sample which searches mail by date with imaplib
> 
> example:
> get email from 01.01.2007 to now
> 
> how can I change imaplib search parameters?
> 
So I had to do the same thing a few years back. I love python but
seriously every author spends less than 1/10 of 1% on doc !

Well So do I .. so take it with a grain of salt. Here is what I did,
MANY YEARS AGO .. I don't even have a IMAP account anymore

t,data = M.uid("SEARCH",None,'(FROM blahblah)')
luid = string.split(data[0])
for uid in luid:
   t,d = M.uid("FETCH",uid,"(BODY[HEADER.FIELDS (DATE SUBJECT TO)])")
   date = d[0][1]

This will get you the dates .. and you can write your own
filter off that .. it's trivial. What isn't so great is
getting every piece of mail and then searching for the needle's in
the haystack.

So I guess this is plan c (just in case you don't get a better answer).
I will watch this one for a "plan a | b" answer.





More information about the Python-list mailing list