Python imaplib can't search by from address
<p>I'm trying to find mails matching a particular FROM address. I've looked at <a href="https://stackoverflow.com/questions/30389333/python-imaplib-search-using-senders-domain">this answer</a> but it doesn't work for me.</p>
<p>Here's the relevant code:</p>
<pre><code>import imaplib

conn = imaplib.IMAP4_SSL(IMAPserver)
conn.login(IMAPuserName, IMAPpassword)
retVal, data = conn.select(&quot;INBOX&quot;)
    if retVal != &quot;OK&quot;:
        &lt;PRINT SOME ERROR MESSAGE&gt;
        sys.exit(1)
</code></pre>
<p>All this works. Here are some variations of the search command that don't work:</p>
<pre><code>retVal, data = conn.search(None, 'UNSEEN HEADER FROM &quot;foo@example.com&quot;')

retVal, data = conn.search(None, 'UNSEEN FROM &quot;foo@example.com&quot;')

retVal, data = conn.search(None, 'FROM &quot;foo@example.com&quot;')

retVal, data = conn.search(None, 'HEADER FROM &quot;foo@example.com&quot;')
</code></pre>
<p>All of these result in errors like these:</p>
<pre><code>    imaplib.error: SEARCH command error: BAD ['Error in IMAP command SEARCH: 
Unexpected string as search key: FROM &quot;foo@example.com&quot;']
</code></pre>
<p>I've referred to the relevant section of the <a href="https://www.rfc-editor.org/rfc/rfc3501.html#page-49" rel="nofollow noreferrer">IMAP v4 RFC</a> but I can't figure out what exactly I'm doing wrong. Any help would be greatly appreciated.</p>