pythonimaplib:can';找不到带有.search的邮件,但如果我对搜索进行硬编码,则会起作用

pythonimaplib:can';找不到带有.search的邮件,但如果我对搜索进行硬编码,则会起作用,python,imaplib,Python,Imaplib,这让我快发疯了。如果我使用此代码: 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 输出为: qatestdata+auto20111104113143@gmail.c

这让我快发疯了。如果我使用此代码:

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
输出为:

qatestdata+auto20111104113143@gmail.com 300 OK ['']
qatestdata+auto20111104113844@gmail.com 0 OK ['19']
换句话说,它找不到我的信息。但是,如果我硬编码如下:

typ, msgnums = gmail.m.uid('search', None, 'To', 'qatestdata+auto20111104113143@gmail.com')
输出为:

qatestdata+auto20111104113143@gmail.com 300 OK ['']
qatestdata+auto20111104113844@gmail.com 0 OK ['19']
(它找到消息。)new_user是一个字符串。我不明白为什么它不起作用

我也尝试过:

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)

但是它也失败了。

我也有同样的问题。我在这里做了一些修改来解决这个问题

我想搜索:

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

上面产生了一个语法错误,你的gmail.m.search调用中的引号有问题。假设
new\u user
是一个变量,它不应该是引号。你的第二行应该是
typ,msgnums=gmail.m.search(无,'TO',新用户)
。我还根据themel的评论更正了引号,但仍然失败。
new\u user
包含什么?new\u user只是一个字符串,qatestdata+auto20111104113143@gmail.com在上面的例子中,字符串是硬编码的,我可以在收件箱中看到它,但由于某些原因,我不能使用变量来指定它。对我来说毫无意义。