Question

I am trying to search "10 minutes older" emails via python imaplib I can search emails with any strings, but not date.

result, data = mail.uid('search', None, '(HEADER Subject "SEARCH_TERM")')

How can I make a search by minutes?

Was it helpful?

Solution

The SEARCH command (SENTSINCE, SINCE, SENTBEFORE, SENTON, BEFORE) works only with with a date, not a date + timestamp. That means the search criteria is limited to DD-MMM-YYYY. You can't include hours or minutes or seconds, just the date.

YES: a1 search since "01-mar-2014"

NO: a1 search since "01-mar-2014 00:00:00"

1 search since "01-mar-2014"

  • SEARCH 498 499 500

1 search since "01-mar-2014 00:00:00"

1 BAD Error in IMAP command SEARCH: Invalid search date parameter

You'll have to look at each message whose message number is returned by the search and apply timestamp filtering in your code. For example:

1 fetch 498:500 internaldate

  • 498 FETCH (INTERNALDATE "04-Mar-2014 13:07:29 -0600")

  • 499 FETCH (INTERNALDATE "05-Mar-2014 07:16:43 -0600")

  • 500 FETCH (INTERNALDATE "05-Mar-2014 07:44:36 -0600")

Or fetch 498:500 (body.peek[header.fields (Date)]) if you prefer to use that date.

-Rick

OTHER TIPS

Base IMAP doesn't allow that. There is an extension, WITHIN, to do it, but it's spottily implemented. See if your server supports WITHIN. If not you'll just have to search for today's mail, retrieve the date fields or INTERNALDATE, and do the fine-tuning yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top