13

I'm trying to fetch all emails whose subject starts with "New Order" but I can't seem to figure it out. Currently I can search for an exact match with a setup like so...

result, data = M.uid('search', None, '(HEADER Subject "Subject Here")')

However this won't retrieve any messages that aren't an exact match. How would I go about doing a partial match?

If it matters I am talking to gmail's imap server(s).

Thanks

Kazurik
  • 183
  • 1
  • 1
  • 6
  • What api/library are you using, also what is `M`? – John Nov 15 '12 at 18:35
  • You also might find http://stackoverflow.com/a/642988/322909 useful. – John Nov 15 '12 at 18:46
  • 1
    I am using the imaplib library and M is an IMAP4 object. I had hoped to only pull down the messages that had the desired subjects and not every single message which I believe is what they are doing in your link although I may be wrong. – Kazurik Nov 15 '12 at 19:04

4 Answers4

22

According to the IMAP RFC SEARCH should do all of its matching as substring matches:

In all search keys that use strings, a message matches the key if the string is a substring of the field. The matching is case-insensitive.

Therefore, a search

M.uid('search', None, 'HEADER Subject "New Order"')

should match all messages where New Order occurs anywhere in the subject. If it is not, you should notify Google that their server does not implement IMAP properly. In the meantime you might try using the SUBJECT key as in

M.uid('search', None, 'SUBJECT "New Order"')

Also, according to Google's IMAP extension documentation you might be able to use the X-GM-RAW key and a gmail search string as in

M.uid('search', None, r'X-GM-RAW "subject:\"New Order\""')
Community
  • 1
  • 1
Geoff Reedy
  • 34,891
  • 3
  • 56
  • 79
  • 2
    I figured out what was going wrong. Google/gmail assumes that all searches are words which means that '(HEADER Subject "Subject Here")' will match things such as "Subject Here Extra words for example" but not "Subject Here5". I'm still not sure how to do a non-word search with gmail or if it is even possible. – Kazurik Nov 15 '12 at 19:16
  • You will have to use gmails extended search syntax. They only support word oriented searches for normal searching. (The x-gm-raw bit he provided) – Max Nov 15 '12 at 20:45
  • 1
    When I run "M.uid('search', None, r'X-GM-RAW "subject:\"New Order\""')" I get the following error: "imaplib.error: UID command error: BAD ['Could not parse command']". Any idea why that might be? – Kazurik Nov 15 '12 at 21:02
  • So substring search isn't possible with gmail over IMAP, and no workaround? – eozzy Dec 12 '14 at 14:21
  • are there any news on this? – Ezer K Apr 17 '23 at 10:36
4

This worked for me:

mail.uid('search', None, r'(X-GM-RAW "subject:\"New Order\"")')
msturdy
  • 10,479
  • 11
  • 41
  • 52
0

This worked for me:

M.uid('search',None, '(SUBJECT "New Order")')

Format for search is (EmailAttribute "your searchstring")

Hope it works for you.

Grizzly
  • 5,873
  • 8
  • 56
  • 109
0

This worked for me:

result, data = m.search(None, 'X-GM-RAW', 'subject: New Order')
Sara
  • 69
  • 2
  • 9