5 # Communicate with mail servers
11 class SecureInbox(object):
12 def __init__(self
, username
, password
):
13 # IMAP SSL for incoming messages
14 self
.mail_in
= imaplib
.IMAP4_SSL("imap.gmail.com", 993)
17 typ
, data
= self
.mail_in
.login(username
, password
)
18 except imaplib
.error
, e
:
19 raise "Login failure: %s" % (e
,)
21 assert typ
== "OK", "imap login returned: %s %s" % (status
, message
)
22 print "Logged in:", typ
, data
24 # Always have "Secure" mailbox selected
25 typ
, num_msgs
= self
.mail_in
.select(mailbox
="Secure")
27 raise ("imap select failure: %s %s" % (typ
, num_msgs
) +
28 "The 'Secure' tag doesn't exist. Tag some of your EMOTP messages" +
29 "using a new label, named 'Secure'")
31 def get_messages(self
):
35 typ
, all_msgs_string
= self
.mail_in
.search(None, 'ALL')
36 except imaplib
.error
, e
:
37 raise "imap search failed: %s" % (e
,)
39 all_msgs
= all_msgs_string
[0].split()
41 typ
, body
= self
.mail_in
.fetch(num
, "(BODY[])")
42 msg
= email
.message_from_string(body
[0][1])
44 body
= str(msg
.get_payload())
45 subject
= msg
.get("Subject")
46 sender
= msg
.get("From")
47 id = msg
.get("Message-ID")
49 #print 'Message %s\n%s\n' % (num, data[0][1])
50 if "--EMOTP_BEGIN--" not in body
:
53 msgs
.append({"body": body
,
66 ms
= SecureInbox("shellreef", getpass
.getpass())
68 for m
in ms
.get_messages():
69 print m
["sender"], m
["subject"]
71 if __name__
== "__main__":