pythonimapimaplib

imaplib finds the full subject string but not partial - how to fix?


I'm using impalib to connect to my gmail:

import imaplib

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('bi@great_company', 'app_password')
mail.select('"[Gmail]/All Mail"', readonly=True)

this finds an email:

mail.search(None, 'SUBJECT "Green Law_Settlement_12242022"')

the email subject is: "Green Law_Settlement_12242022.xlsx"

but this does not return any match:

mail.search(None, 'SUBJECT "Green Law_Settlement_122"')

I need all the mails where the subject starts with "Green Law_Settlement_" any way of doing that?


Solution

  • IMAP says searches match substrings, and case-insensitively, but it doesn't define "substring". I think the author of the RFC assumed that strings are a sequence of characters, as they commonly are. However, gmail says that strings are a sequence of words, and a word is… the precise definition doesn't matter. Color and colour may be regarded as the same word, as may colours or colouring.

    Gmail's way isn't unique. The IMAP servers that use SOLR or Lucene internally will work somewhat like gmail, for example. It makes a lot of sense for the code that implements searching inside the server.

    The conclusion is to try to use substrings that consist of entire words. Searching for "green law" should be fine with all servers, for "en law_set" is risky.