简体   繁体   中英

python imap mail search with minutes

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM