first whack at reading / printing emails

workspace
Brendan Howell 8 years ago
parent f3f8f7b55c
commit 27857a5a31

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Screenless Office - Mailroom</title>
<meta charset="utf-8" />
<style>
body {
font-family: "FreeUniversal";
font-size: 11pt;
}
#main {
margin-top: 2em;
}
#content {
float: left;
width: 63%;
padding-right: 3%;
}
#menu {
width: 30%;
float: left;
padding-right: 3%;
}
.cmd {
clear: both;
width: 100%;
}
</style>
</head>
<div id="container">
<div id="header">
<div class="env-info">From: ${msg.fromstr}</div>
<div class="env-info">CC: ${msg.cc}</div>
<div class="env-info">Subject: ${msg.subject}</div>
<div class="env-info">Date: ${msg.date}</div>
<div class="env-info">To: ${msg.tostr}</div>
</div>
<div id="main">
<div id="content">
${msg.content}
</div>
<div id="menu">
<div class="cmd">Mark as Unread</div>
<div class="cmd">Delete</div>
<div class="cmd">Mark as SPAM</div>
<div class="cmd">Reply</div>
<div class="cmd">Reply All</div>
<div class="cmd">Print a Copy</div>
</div>
</div>
</div>
</html>

@ -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):

Loading…
Cancel
Save