5 # Communicate with mail servers
12 class SecureInbox(object):
13 def __init__(self
, username
, password
):
14 # IMAP SSL for incoming messages
15 self
.mail_in
= imaplib
.IMAP4_SSL("imap.gmail.com", 993)
17 self
.username
= username
20 typ
, data
= self
.mail_in
.login(username
, password
)
21 except imaplib
.error
, e
:
22 raise "Login failure: %s" % (e
,)
24 assert typ
== "OK", "imap login returned: %s %s" % (status
, message
)
25 print "Logged in:", typ
, data
27 # Always have "Secure" mailbox selected
28 typ
, num_msgs
= self
.mail_in
.select(mailbox
="Secure")
30 raise ("imap select failure: %s %s" % (typ
, num_msgs
) +
31 "The 'Secure' tag doesn't exist. Tag some of your EMOTP messages" +
32 "using a new label, named 'Secure'")
34 # Outgoing mail, SMTP server
35 self
.mail_out
= smtplib
.SMTP("smtp.gmail.com", 25)
36 ret
= self
.mail_out
.ehlo()
37 assert ret
[0] == 250, "SMTP server EHLO failed: %s" % (ret
,)
39 ret
= self
.mail_out
.starttls()
40 assert ret
[0] == 220, "SMTP server STARTTLS failed: %s" % (ret
,)
42 ret
= self
.mail_out
.ehlo()
43 assert ret
[0] == 250, "SMTP server EHLO over TLS failed: %s" % (ret
,)
45 ret
= self
.mail_out
.noop()
46 assert ret
[0] == 250, "SMTP server NOOP failed: %s" % (ret
,)
48 ret
= self
.mail_out
.login(username
, password
)
49 assert ret
[0] == 235, "SMTP server login failed: %s" % (ret
,)
50 print "Logged in to SMTP server: %s" % (ret
,)
52 def get_messages(self
):
56 typ
, all_msgs_string
= self
.mail_in
.search(None, 'ALL')
57 except imaplib
.error
, e
:
58 raise "imap search failed: %s" % (e
,)
60 all_msgs
= all_msgs_string
[0].split()
62 typ
, body
= self
.mail_in
.fetch(num
, "(BODY[])")
63 msg
= email
.message_from_string(body
[0][1])
65 body
= str(msg
.get_payload())
66 subject
= msg
.get("Subject")
67 sender
= msg
.get("From")
68 id = msg
.get("Message-ID")
70 #print 'Message %s\n%s\n' % (num, data[0][1])
71 if "--EMOTP_BEGIN--" not in body
:
74 msgs
.append({"body": body
,
83 """Fetch messages from server."""
84 self
.msgs
= self
.get_messages()
85 return iter(self
.msgs
)
87 def __getitem__(self
, k
):
88 """Lookup a message of a given number. Messages
89 must have been previous fetched from server up by __iter__"""
92 def send(self
, to
, subject
, body
):
93 from_address
= "%s@gmail.com" % (self
.username
,)
94 return self
.mail_out
.sendmail(from_address
,
97 "From: %s" % (from_address
,),
99 "Subject: %s" % (subject
,),
105 self
.mail_in
.logout()
109 ms
= SecureInbox("shellreef", getpass
.getpass())
111 for m
in ms
.get_messages():
112 print m
["sender"], m
["subject"]
114 if __name__
== "__main__":