--- /dev/null
+from imapclient import IMAPClient\r
+import time\r
+\r
+HOST = 'imap.host.com'\r
+USERNAME = 'someuser'\r
+PASSWORD = 'password'\r
+ssl = True\r
+\r
+server = IMAPClient(HOST, use_uid=True, ssl=ssl)\r
+\r
+server.login(USERNAME, PASSWORD)\r
+\r
+select_info = server.select_folder('INBOX')\r
+print select_info\r
+print\r
+\r
+idling = True\r
+\r
+def callback(resp, arg):\r
+ global idling\r
+ \r
+ print "Something happened, or timeout was reached"\r
+ print\r
+ print "Callback response:"\r
+ print resp\r
+ print\r
+ print "The callback function received arg: ", arg\r
+ print\r
+ idling = False\r
+\r
+server.idle(timeout=5, callback=callback, cb_arg="Hello future self!")\r
+print "Began idling"\r
+\r
+while idling:\r
+ print "..still idling.."\r
+ time.sleep(1)\r
+ \r
+print server.logout()
\ No newline at end of file
# Please see http://en.wikipedia.org/wiki/BSD_licenses
import re
-import imaplib
import response_lexer
from operator import itemgetter
import warnings
-#imaplib.Debug = 5
+try:
+ import imaplib2 as imaplib
+ imaplib2 = True
+except ImportError:
+ imaplib2 = False
+ import imaplib
+
+#imaplib.Debug = 5
+
import imap_utf7
from fixed_offset import FixedOffset
"""
typ, data = self._imap.select(self._encode_folder_name(folder), readonly)
self._checkok('select', typ, data)
- return self._process_select_response(self._imap.untagged_responses)
+ if imaplib2:
+ untagged_responses = self._response_to_dict(self._imap.untagged_responses)
+ else:
+ untagged_responses = self._imap.untagged_responses
+ return self._process_select_response(untagged_responses)
+
+ def _response_to_dict(self, resp):
+ dictresp = {}
+
+ if len(resp) > 0:
+ for item in resp:
+ dictresp[item[0]] = item[1]
+ return dictresp
def _process_select_response(self, resp):
out = {}
out[key] = value
return out
+ def idle(self, timeout=None, **kwargs):
+ """Put server into IDLE mode.
+ IDLE mode will end when the server notifies of a change, the sever
+ reaches the timeout, or another IMAP4 command is scheduled.
+
+ @param timeout: timeout for the IDLE command in seconds (default: 29 mins)
+ @param callback: Function to be called when IDLE mode ends. If a callback
+ is supplied the idle function will be asynchronous, returning immediately.
+ @param cb_arg: An optional argument that will be passed to the callback function.
+ @return: Server response. (None if callback is specified).
+ """
+ if imaplib2:
+ if 'callback' in kwargs:
+ callback = kwargs.pop('callback')
+ def _wrapped_callback(resp):
+ typ, data = resp[0]
+ self._checkok('idle', typ, data)
+ # if there's a cb_arg, pass it along
+ cb_arg = resp[1]
+ if cb_arg:
+ callback(data[0], cb_arg)
+ else:
+ callback(data[0])
+ self._imap.idle(timeout, callback=_wrapped_callback, **kwargs)
+ else:
+ typ, data = self._imap.idle(timeout)
+ self._checkok('idle', typ, data)
+ return data[0]
+ else:
+ raise self.Error('The imaplib2 module is required to use IDLE')
+
def folder_status(self, folder, what=None):
"""Requests the status from folder.
tag = self._imap._command('UID', 'FETCH', msg_list, parts_list, modifiers_list)
else:
tag = self._imap._command('FETCH', msg_list, parts_list, modifiers_list)
- typ, data = self._imap._command_complete('FETCH', tag)
+ if imaplib2:
+ typ, data = self._imap._command_complete(tag, 'FETCH')
+ else:
+ typ, data = self._imap._command_complete('FETCH', tag)
self._checkok('fetch', typ, data)
typ, data = self._imap._untagged_response(typ, data, 'FETCH')
return parse_fetch_response(data)