diff --git a/README.md b/README.md index c71c7f2..2b0802c 100644 --- a/README.md +++ b/README.md @@ -24,5 +24,33 @@ Usage + # Every message is converted to a dictionary with the following keys: + + { + 'MesssageID': '22c74902-a0c1-4511-804f2-341342852c90', + 'From': { + 'Name': 'John Doe', + 'Email': 'jonhdoe@email.com' + }, + 'To': { + 'Name': 'Martin Rusev', + 'Email': 'martin@amon.cx' + }, + 'Date': 'Mon, 22 Jul 2013 23:21:39 +0000 (UTC)', + 'TextBody': ['ASCII'], + 'Subject': 'This is a message' + + 'Headers': [{ + 'Name': 'Received-SPF', + 'Value': 'pass (google.com: domain of bounces+......;' + }, { + 'Name': 'MIME-Version', + 'Value': '1.0' + }], + + } + + + diff --git a/mailbox/__init__.py b/mailbox/__init__.py index ddf38e0..c494c06 100644 --- a/mailbox/__init__.py +++ b/mailbox/__init__.py @@ -12,23 +12,57 @@ class MailBox(object): def parse_email(self, raw_email): email_message = email.message_from_string(raw_email) - headers = dict(email_message.items()) + maintype = email_message.get_content_maintype() - message = [] + text_body = [] if maintype == 'multipart': - for part in email_message.get_payload(): - if part.get_content_maintype() == 'text': - message.append(part.get_payload(decode=True)) + for part in email_message.walk(): + if part.get_content_type() == "text/plain": + text_body.append(part.get_payload(decode=True)) elif maintype == 'text': - message.append(email_message.get_payload(decode=True)) + text_body.append(email_message.get_payload(decode=True)) + + email_dict = dict(email_message.items()) + + from_dict = {} + from_ = email.utils.parseaddr(email_dict['From']) + if len(from_) == 2: + from_dict = {'Name': from_[0], 'Email': from_[1]} + + to_dict = {} + to_ = email.utils.parseaddr(email_dict['To']) + if len(to_) == 2: + to_dict = {'Name': to_[0], 'Email': to_[1]} + + subject = email_dict.get('Subject', None) + date = email_dict.get('Date', None) + message_id = email_dict.get('Message-ID', None) + + # Get the headers + headers = [] + headers_keys = ['Received-SPF', + 'MIME-Version', + 'X-Spam-Status', + 'X-Spam-Score'] + + for key in headers_keys: + header_value = email_dict.get(key) + + if header_value: + headers.append({'Name': key, + 'Value': header_value}) return { - 'message': message, - 'headers': headers, - 'maintype': maintype + 'MesssageID': message_id, + 'From': from_dict, + 'To': to_dict, + 'Subject': subject, + 'Date': date, + 'TextBody': text_body, + 'Headers': headers } def fetch_by_uid(self, uid): @@ -58,4 +92,3 @@ class MailBox(object): message, data = self.connection.uid('search', None, "UNSEEN") return self.fetch_list(data) - \ No newline at end of file