5 # Communicate with mail servers
11 class MailServers(object):
12 def __init__(self
, username
, password
):
13 self
.mail_in
= imaplib
.IMAP4_SSL("imap.gmail.com", 993)
16 typ
, data
= self
.mail_in
.login(username
, password
)
17 except imaplib
.error
, e
:
18 raise "Login failure: %s" % (e
,)
20 assert typ
== "OK", "imap login returned: %s %s" % (status
, message
)
21 print "Logged in:", typ
, data
23 # Always have "Secure" mailbox selected
24 typ
, num_msgs
= self
.mail_in
.select(mailbox
="Secure")
26 raise ("imap select failure: %s %s" % (typ
, num_msgs
) +
27 "The 'Secure' tag doesn't exist. Tag some of your EMOTP messages" +
28 "using a new label, named 'Secure'")
30 def get_messages(self
):
34 typ
, all_msgs_string
= self
.mail_in
.search(None, 'ALL')
35 except imaplib
.error
, e
:
36 raise "imap search failed: %s" % (e
,)
38 all_msgs
= all_msgs_string
[0].split()
40 typ
, body
= self
.mail_in
.fetch(num
, "(BODY[])")
41 msg
= email
.message_from_string(body
[0][1])
43 body
= str(msg
.get_payload())
44 subject
= msg
.get("Subject")
45 sender
= msg
.get("From")
46 id = msg
.get("Message-ID")
48 #print 'Message %s\n%s\n' % (num, data[0][1])
49 if "--EMOTP_BEGIN--" not in body
:
52 msgs
.append({"body": body
,
65 ms
= MailServers("shellreef", getpass
.getpass())
67 for m
in ms
.get_messages():
68 print m
["sender"], m
["subject"]
70 if __name__
== "__main__":