diff --git a/screenless/bureau/mailroom/email.html b/screenless/bureau/mailroom/email.html
new file mode 100644
index 0000000..2edff32
--- /dev/null
+++ b/screenless/bureau/mailroom/email.html
@@ -0,0 +1,52 @@
+
+
+
+ The Screenless Office - Mailroom
+
+
+
+
+
diff --git a/screenless/bureau/mailroom/mailroom.py b/screenless/bureau/mailroom/mailroom.py
index 790a338..a8e1a70 100644
--- a/screenless/bureau/mailroom/mailroom.py
+++ b/screenless/bureau/mailroom/mailroom.py
@@ -1,4 +1,5 @@
-import email.header
+import email
+from email.header import decode_header, make_header
import imaplib
import imapclient
@@ -82,12 +83,39 @@ class MailRoom(Bureau):
shortcode, _ = data.split(".")
imap_id = self.get_imap_id(shortcode)
self._connect_imap()
- resp = self.imapserv.fetch([imap_id], ['ENVELOPE', 'INTERNALDATE'])
- msg = Message()
- # msg.to = resp[imap_id][]
- self.imapserv.add_flags(imap_id, [imapclient.SEEN])
+ resp = self.imapserv.fetch([imap_id],
+ ['INTERNALDATE', 'RFC822'])
+
+ internaldate = resp[imap_id][b'INTERNALDATE']
+
+ msg_data = resp[imap_id][b'RFC822'].decode('utf-8')
+ msg_obj = email.message_from_string(msg_data)
+ print("message fields:", msg_obj.keys())
- # self.print_full("email.html", msg=msg, shortcode=shortcode)
+ # format and tidy header data
+ msg = Message()
+ msg.fromstr = make_header(decode_header(msg_obj['From']))
+ msg.tostr = make_header(decode_header(msg_obj['To']))
+ msg.subject = make_header(decode_header(msg_obj['Subject']))
+ if 'Cc' in msg_obj:
+ msg.cc = make_header(decode_header(msg_obj['Cc']))
+ else:
+ msg.cc = None
+ msg.date = internaldate
+ msg.shortcode = shortcode
+ msg.attachments = []
+
+ # TODO: should use msg_obj.get_body and deal with HTML
+ msg.content = ""
+
+ # extract other sub-messages / attachments
+ for part in msg_obj.walk():
+ # TODO: save interesting attachments to files
+ # should clean these up on delete
+ if part.get_content_type() == "text/plain":
+ msg.content = part.get_payload(decode=True).decode("utf-8")
+
+ self.print_full("email.html", msg=msg, shortcode=shortcode)
@add_command("d", "Delete email")
def delete(self, data):