def downloadMails(emailId, accessTokenKey, accessTokenSecret, fetchDate): gMailFetch = None gMailService = None gMailXOAuthString = None userIdentity = emailId oAuthConsumer = xoauth.OAuthEntity(gMailConfig.gMailConsumerKey, gMailConfig.gMailConsumerSecret) oAuthAccess = xoauth.OAuthEntity(accessTokenKey, accessTokenSecret) gMailService = eMailService.eMailService(userIdentity, oAuthConsumer, oAuthAccess, gMailProtocol, gMailProvider, siteConfig.debugEnabled) gMailXOAuthString = gMailService.doGenerateXOAuthString() gMailRepoInst = repositoryIface.repositoryIface(gMailRepoType, gMailRepoRoot, siteConfig.debugEnabled) gMailParserInst = eMailParser.eMailParser() if gMailXOAuthString is not None: gMailFetch = eMailFetch.eMailFetch(gMailProtocol, gMailProvider, gMailXOAuthString, siteConfig.debugEnabled) #Connect to GMAIL Service Provider. gMailFetch.doConnect() #Select "All Mail" mailbox. gMailFetch.doSelectMailbox('[Gmail]/All Mail') ''' Connect to DataStore. ''' gMailRepoInst.doConnectToDataStore() gMailRepoInst.doSelectDataStore(emailId) ''' Fetch the mails. The result is a tuple (Total number of mails, Fetched mail count, Fetched mail list). ''' searchAction = 'ALL' if fetchDate is not None: searchAction = '(SINCE "' + fetchDate + '")' result = gMailFetch.getMails(0, 1, searchAction, 'CONT', 'HEADER')[ 0 ] if result[ 0 ] != -1: totalMails = result[ 0 ] remMails = totalMails startIdx = 0 try: while remMails > 0: debugTraceInst.doPrintTrace('StartIdx: %d, remMails: %d'%(startIdx, remMails), sys.exc_info()[2], True) result = gMailFetch.getMails(startIdx, 0, searchAction, 'CONT', 'FULL') remMails -= result[ 0 ][ 1 ] startIdx += result[ 0 ][ 1 ] ''' Write the fetched data to repository. Currently, each mail header is stored in a separate file. Only headers are being fetched and stored. ''' for mailEntry in result[ 0 ][ 2 ]: mailHeader = email.message_from_string(mailEntry[ 1 ]) messageId = email.header.decode_header(mailHeader[ 'Message-ID' ])[ 0 ][ 0 ] cleanMessageId = gMailParserInst.doCleanupForFilename(messageId) entryName = emailId + cleanMessageId + siteConfig.fileSuffix #Transform the message into a storage format. gMailRepoInst.doUpdateItem(entryName, mailEntry[ 1 ]) except: debugTraceInst.doPrintTrace(errorStringsInst.getFailedRequestError(), sys.exc_info()[2]) else: debugTraceInst.doPrintTrace(errorStringsInst.getFetchFailedError()) gMailRepoInst.doDisconnect() gMailFetch.doDisconnect() else: debugTraceInst.doPrintTrace(errorStringsInst.getXOAuthStringNoneError())