Search code examples
pythonimapimaplib

Python imaplib search 'NOT' keyword


I've been learning to use the python imaplib library for a project I'm working on,but I'm seeing some behavior that has me stumped. When I try to search with 'NOT FROM' it returns the same as 'FROM', as if the not wasn't even there. My assumption is that I am just putting the command in wrong, but I've searched for a few examples and every one I have found seems to do it the way I am.

import imaplib
mail = imaplib.IMAP4_SSL(host)
mail.login(email, pw)
mail.select(mailbox)
# The following two work as expeceted
# and give different results
mail.uid('search', None, '(NOT NEW)')
mail.uid('search', None, '(NEW)')
# However the following two give the same
mail.uid('search', None, '(FROM gmail)')
mail.uid('search', None, '(NOT FROM gmail)')

I've tried every way of inputing it I can think of, but none of them seem to work. I've checked both RFC2060, the one imaplib says it is based on and some of the more recent ones.

$ python --version
Python 2.7.5+

Does anyone see where I am messing up or is also seeing this issue? There is very little out there about this particular library it seems


EDIT: I've looked in to this a little further and it appears to be a problem specifically with the imap server I am connecting to.. After trying to connect over openssl with

$ openssl s_client -connect imap.mail.yahoo.com:993 -crlf

and logging in and trying

UID SEARCH FROM "gmail"
UID SEARCH NOT FROM "gmail"

I get the same behavior.

However when I connect to a gmail account

$ openssl s_client -connect imap.gmail.com:993 -crlf

and try

UID SEARCH FROM "gmail"
UID SEARCH NOT FROM "gmail"

I get the expected behavior.

I guess this is "SOLVED" then


Solution

  • I've looked in to this a little further and it appears to be a problem specifically with the imap server I am connecting to.. After trying to connect over openssl with

    $ openssl s_client -connect imap.mail.yahoo.com:993 -crlf
    

    and logging in and trying

    UID SEARCH FROM "gmail"
    UID SEARCH NOT FROM "gmail"
    

    I get the same behavior.

    However when I connect to a gmail account

    $ openssl s_client -connect imap.gmail.com:993 -crlf
    

    and try

    UID SEARCH FROM "gmail"
    UID SEARCH NOT FROM "gmail"
    

    I get the expected behavior.

    I guess this is "SOLVED" then