2011-11-04 3 views
0

이것은 나를 괴롭 히고 있습니다. 이 코드를 사용하는 경우 :Python imaplib : .search를 사용하여 메시지를 찾을 수 없습니다. 검색을 하드 코드하면 작동합니다.

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 

출력은 다음과 같습니다 즉

[email protected] 300 OK [''] 

이 내 메시지를 찾는 아니에요. 하지만,이 같은 내가 하드 코딩을하는 경우 :

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

출력은 다음과 같습니다

[email protected] 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) 

을하지만 너무 실패

는 I도 노력했다.

+0

위는 구문 에러를 생산, 뭔가는 gmail.m.search 통화에서 따옴표에 문제가 있습니다. – themel

+0

'new_user'가 변수라고 가정하면, 그것은 따옴표가되어서는 안됩니다. 두 번째 줄에 'typ, msgnums = gmail.m.search (None,'TO ', new_user)'라고 읽어야합니다. – brc

+0

new_user로 시도했지만 아직 메시지를 찾지 못했습니다. 나는 또한 themel의 의견 당 따옴표를 정정하고 아직도 실패한다. –

답변

1

나는 동일한 문제가있었습니다. 나는 따끔 따옴표에 약간 변화에 그것을 해결한다.

resp, items = m.search(None, '(FROM "*" SUBJECT "<[email protected]> test.sh > /dev/null")') 

하드 코딩 작업을하지만 잘 보이지 않지만 루프 작동하지 않았다 :

는 그를 검색 할 수 있습니다.

email_subjects_with_del = [ "<[email protected]> 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) 

그러나합니다 ('따옴표없이) 작동

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

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