Changes between Version 216 and Version 217 of WikiStart


Ignore:
Timestamp:
07/20/2012 03:43:27 PM (6 years ago)
Author:
Jean-Paul Calderone
Comment:

An IMAP4 client example

Legend:

Unmodified
Added
Removed
Modified
  • WikiStart

    v216 v217  
    123123    <li><a href="#webserver">Web server</a></li>
    124124    <li><a href="#pubsubserver">Publish/subscribe</a></li>
     125    <li><a href="#imap4client">IMAP4 Client</a></li>
    125126</ul>
    126127}}}
     
    217218}}}
    218219
     220{{{
     221#!div class="tab-pane" id="imap4client"
     222
     223Twisted includes a sophisticated IMAP4 client library.
     224
     225{{{
     226#!python
     227from twisted.internet import reactor, protocol, defer, endpoints
     228from twisted.mail import imap4
     229from twisted.python import log, failure
     230
     231@defer.inlineCallbacks
     232def main(username="alice", password="secret",
     233         strport="ssl:host=example.com:port=993"):
     234    endpoint = endpoints.clientFromString(reactor, strport)
     235    factory = protocol.Factory()
     236    factory.protocol = imap4.IMAP4Client
     237    try:
     238        client = yield endpoint.connect(factory)
     239        yield client.login(username, password)
     240        yield client.select('INBOX')
     241        info = yield client.fetchEnvelope(imap4.MessageSet(1))
     242        print info[1]['ENVELOPE'][1]
     243    except:
     244        log.err(failure.Failure(), "IMAP4 client interaction failed")
     245    reactor.stop()
     246
     247import sys
     248main(*sys.argv[1:])
     249reactor.run()
     250}}}
     251
     252Give this a try, supplying your IMAP4 username, password, and [http://twistedmatrix.com/documents/current/core/howto/endpoints.html#auto7 client endpoint description] for your IMAP4 server.  You'll see the subject of the first message in your mailbox printed.
     253
     254See the TwistedMail documentation for more information.
     255
     256}}}
    219257
    220258}}}