~daves/gnome-gmail/ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
"""
Connect to an IMAP4 server with Twisted.
---------------------------------------
Run like so:
    $ python twisted_imap4_example.py

This example is slightly Gmail specific,
in that SSL is required, and the correct
server name and ports are set for Gmail.

This solution originally by Phil Mayers, on twisted-python:
http://twistedmatrix.com/pipermail/twisted-python/2009-June/019793.html
"""

from twisted.internet import reactor, protocol, defer
from twisted.mail import imap4
from twisted.internet import ssl

USERNAME = 'dsteele'
PASSWORD = 'agendick'

# Gmail specific:
SERVER = 'imap.gmail.com'
PORT = 993
# Gmail requires you connect via SSL, so
#we pass the follow object to 'someclient.connectSSL':
contextFactory = ssl.ClientContextFactory()

def mailboxes(list):
    print list
    for flags,sep,mbox in list:
        print mbox

def loggedin(res, proto):
    d = proto.list('','*')
    d.addCallback(mailboxes)
    return d

def connected(proto):
    print "connected", proto
    d = proto.login(USERNAME, PASSWORD)
    d.addCallback(loggedin, proto)
    d.addErrback(failed)
    return d

def failed(f):
    print "failed", f
    return f

def done(_):
    reactor.callLater(0, reactor.stop)

def main():
    c = protocol.ClientCreator(reactor, imap4.IMAP4Client)
    d = c.connectSSL(SERVER, PORT, contextFactory)
    d.addCallbacks(connected, failed)
    d.addBoth(done)

reactor.callLater(0, main)
reactor.run()