search mail by date with imaplib

rweth rweth at cisco.com
Sat Jan 6 17:31:45 EST 2007


Jean-Paul Calderone wrote:
> On Sat, 06 Jan 2007 11:49:17 -0800, rweth <rweth at cisco.com> wrote:
>> 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.
>>
> 
> The SEARCH command supports quite a few kinds of criteria.  Several which
> may be relevant here are BEFORE, SINCE, SENTBEFORE, and SENTSINCE.  Using
> Twisted's IMAP4 code, you can format a query like this:
> 
>    >>> from twisted.mail.imap4 import Query
>    >>> Query(after='01-Jan-2007')
>    '(AFTER "01-Jan-2007")'
>    >>>
> 
> Since documentation was brought up, I'll point out Query's docs too, since
> that might help shed some further light on what's possible.
> 
> http://twistedmatrix.com/documents/current/api/twisted.mail.imap4.html#twisted.mail.imap4.Query 
> 
> 
> Jean-Paul
Wow looks like twisted took Imap4 and made it better!
I think I had better read up on twisted .. this looks quite
good (Plan A for sure).



More information about the Python-list mailing list