Aaron
Aaron

Reputation: 2305

Python imaplib: can't find a message with .search, but works if I hardcode the search

This is driving me nuts. If I use this code:

while msgnums == ['']:  # wait until message list from server isn't empty
    typ, msgnums = gmail.m.uid('search', None, 'To', new_user)
    print '\n', new_user, sec_waiting, typ, msgnums

The output is:

[email protected] 300 OK ['']

In other words, it's not finding my message. BUT, if I hard code it like this:

typ, msgnums = gmail.m.uid('search', None, 'To', '[email protected]')

The output is:

[email protected] 0 OK ['19']

(It finds the message.) new_user is a string. I don't understand why it's not working.

I have also tried:

search_string = '(To \"' + created_username + '\")'
while msg_uid == ['']:  # wait until message list from server isn't empty
    resp, msg_uid = gmail.m.search(None, search_string)

But it fails too.

Upvotes: 0

Views: 503

Answers (1)

ThanasisN
ThanasisN

Reputation: 103

I had the same problem. I solve it with some changes at the sting quotes.

I want to search that:

resp, items = m.search(None, '(FROM "*" SUBJECT "<root@doire> test.sh > /dev/null")')

The hard coded works but the loop didn't work although it seem right.

    email_subjects_with_del = [ "<root@doire> test.sh > /dev/null" ]

    for sub in email_subjects_with_del:
        text = "%s%s%s" % ("'(FROM \"*\" SUBJECT \"", sub, "\")'")

        print text
        resp, items = m.search(None, text )

But this works (without the ' quotes)

    for sub in email_subjects_with_del:
        text = "%s%s%s" % ("(FROM \"*\" SUBJECT \"", sub, "\")")

        print text
        resp, items = m.search(None, text )

Upvotes: 1

Related Questions