[Twisted-Python] IMAP4 Client extrange behavior

César García celord at gmail.com
Mon Feb 22 11:25:58 EST 2010

Hello all, I have this imap4 client http://www.pastebin.com/m4e387f1a
most of the code is barrowed from a IRC friend, bob_f , it works fine
but as soon as more that 1 email arrrives to the inbox, it only prints
the first new messages but not the others,  I wonder what am I doing

Thanks a lot

Here is the code:

#!/usr/bin/env python

Client de IMAP4 que descarga contenido del INBOX de una cuenta en especifico
para luego extraer el numero de telefono que debe venir en los correos enviados

import StringIO
import sys
from Config import retCredentials

from twisted.internet.task import LoopingCall
from twisted.internet import protocol
from twisted.internet import defer
from twisted.mail import imap4
from twisted.python import util
from twisted.python import log
from twisted.internet import reactor
debug = 1

class IMAP4Client(imap4.IMAP4Client):

    def serverGreeting(self,caps):
        Metodo llamado cuando el servidor contesta
        if debug: print "On serverGreeting"

        self.serverCapabilities = caps
        if self.greetDeferred is not None:
            d, self.greetDeferred = self.greetDeferred, None

    def cbLogin(self, proto):
        Callback to IMAP login
        if debug: print "On Login"

        login =  self.login(self.factory.username, self.factory.password)

    def startPolling(self, proto):
        Callback to poll every x seconds
        call = LoopingCall(self.selectMailbox,mailbox="INBOX")
        call.start(20, now=True)

    def selectMailbox(self, mailbox):
        Select the mailbox to examin
        if debug: print "On selectMailbox"

        mailbox = self.factory.mailbox
        return self.select(mailbox).addCallback(self.cbSelectSuccess)

    def cbSelectSuccess(self, selected):
        Examine the INBOX mailbox for new mails

        if debug: print "On cbSelectSuccess"

        self.messageCount = selected['EXISTS']
        print "Messages: ", self.messageCount

        unseen = selected['EXISTS'] - selected['RECENT']

        if selected['RECENT'] == 0:
            print "No new messages"

        return self.fetchMessage("%s:*" % (unseen)

    def cbProcMessages(self,messages):

        print messages

class IMAP4ClientFactory(protocol.ClientFactory):

    protocol = IMAP4Client

    def __init__(self, username, password,  onConn):

        self.username = username
        self.password = password
        self.mailbox = 'INBOX'
        self.onConn = onConn

    def buildProtocol(self,addr):
        if debug: print "On buildProtocol"
        p = self.protocol()
        p.factory = self
        p.greetDeferred = self.onConn
        auth = imap4.CramMD5ClientAuthenticator(self.username)

        return p

    def clientConectionFailed(self, connector, reason):
        d, self.onConn = self.onConn, None

def ebConnection(reason):

PORT = 143

def main():
    credentials = retCredentials()
    hostname = credentials['server']
    username = credentials['mailbox']
    password = util.getPassword('IMAP4 Password: ')

    onConn = defer.Deferred(

    factory = IMAP4ClientFactory(username, password, onConn)

    reactor.connectTCP(hostname, PORT, factory)

if  __name__ == "__main__":


More information about the Twisted-Python mailing list