'Extract body as HTML and attachments from the mail message'

def _message_extract_payload(self, message, save_original=False):

    attachments = []
    body = u''
    if save_original:
        attachments.append(self._Attachment('original_email.eml', message.as_string(), {}))
    if ((not message.is_multipart()) or message.get('content-type', '').startswith('text/')):
        encoding = message.get_content_charset()
        body = message.get_payload(decode=True)
        body = tools.ustr(body, encoding, errors='replace')
        if (message.get_content_type() == 'text/plain'):
            body = tools.append_content_to_html(u'', body, preserve=True)
    else:
        alternative = False
        mixed = False
        html = u''
        for part in message.walk():
            if (part.get_content_type() == 'multipart/alternative'):
                alternative = True
            if (part.get_content_type() == 'multipart/mixed'):
                mixed = True
            if (part.get_content_maintype() == 'multipart'):
                continue
            filename = part.get_param('filename', None, 'content-disposition')
            if (not filename):
                filename = part.get_param('name', None)
            if filename:
                if isinstance(filename, tuple):
                    filename = email.utils.collapse_rfc2231_value(filename).strip()
                else:
                    filename = tools.decode_smtp_header(filename)
            encoding = part.get_content_charset()
            if (filename and part.get('content-id')):
                inner_cid = part.get('content-id').strip('><')
                attachments.append(self._Attachment(filename, part.get_payload(decode=True), {'cid': inner_cid}))
                continue
            if (filename or part.get('content-disposition', '').strip().startswith('attachment')):
                attachments.append(self._Attachment((filename or 'attachment'), part.get_payload(decode=True), {}))
                continue
            if ((part.get_content_type() == 'text/plain') and ((not alternative) or (not body))):
                body = tools.append_content_to_html(body, tools.ustr(part.get_payload(decode=True), encoding, errors='replace'), preserve=True)
            elif (part.get_content_type() == 'text/html'):
                append_content = ((not alternative) or (html and mixed))
                html = tools.ustr(part.get_payload(decode=True), encoding, errors='replace')
                if (not append_content):
                    body = html
                else:
                    body = tools.append_content_to_html(body, html, plaintext=False)
            else:
                attachments.append(self._Attachment((filename or 'attachment'), part.get_payload(decode=True), {}))
    (body, attachments) = self._message_extract_payload_postprocess(message, body, attachments)
    return (body, attachments)
