def get_nested_payload(mime_message): """ Returns a single message object from a list of text content and attachments in a MIME message, after filtering out unwanted content. Also handles nested content like forwarded messages. :param mime_message: The MIME message to traverse looking for content :return: A list of plain-text email bodies and a list of base-64 attachments (if any) """ return_message = EmailMessage() return_message.subject = mime_message.get('Subject') return_message.sender = clean_sender(mime_message.get('From')) return_message.recipient = clean_recipient(mime_message.get('To')) return_message.date = parse(mime_message.get('Date')) for sub_message in mime_message.walk(): content_type = sub_message.get_content_type() disposition = sub_message.get('Content-Disposition') if content_type == 'text/plain' and disposition is None: x = unicode(sub_message.get_payload()) return_message.append_body(x) elif content_type in _ignored_content_types and disposition is None: pass # throw away contents we don't want else: return_message.add_attachment(sub_message.get_payload(), content_type=content_type, filename=disposition) return return_message