2017-04-11 73 views
0

有人可以指出如何正確使用python中的imaplib進行搜索。電子郵件服務器是Microsoft Exchange - 似乎有問題,但我想從python/imaplib方面提供解決方案。 https://github.com/barbushin/php-imap/issues/128python3 imaplib搜索功能編碼

我至今使用:

import imaplib 
M = imaplib.IMAP4_SSL(host_name, port_name) 
M.login(u, p) 
M.select() 
s_str = 'hello' 
M.search(s_str) 

而且我得到以下錯誤:

>>> M.search(s_str) 
('NO', [b'[BADCHARSET (US-ASCII)] The specified charset is not supported.']) 
+0

您也可能想給IMAPClient一展身手。如果您將搜索條件作爲unicode字符串傳遞,它會爲您處理大部分搜索編碼方面的問題。 項目:https://github.com/mjs/imapclient; 相關文檔:http://imapclient.readthedocs.io/en/stable/#imapclient.IMAPClient.search –

回答

1

search採用兩個或多個參數,編碼和搜索規範。您可以傳遞None作爲編碼,不指定一個。 hello不是有效的字符集。

您還需要指定要搜索的內容:IMAP具有在RFC3501§6.4.4中詳細描述的複雜搜索語言; imaplib不提供高級接口。

所以,用這兩個記住,你需要做的是這樣的:

search(None, 'BODY', '"HELLO"') 

search(None, 'FROM', '"HELLO"')