def extract_email_body()

in workmail-wordpress-python/src/utils.py [0:0]


def extract_email_body(parsed_email):
    """
    Extract email message content of type "text/html" from a parsed email
    Parameters
    ----------
    parsed_email: email.message.Message, required
        The parsed email as returned by download_email
    Returns
    -------
    string
        string containing text/html email body decoded with according to the Content-Transfer-Encoding header
        and then according to content charset.
    None
        No content of type "text/html" is found.
    """
    text_content = None
    text_charset = None
    if parsed_email.is_multipart():
        # Walk over message parts of this multipart email.
        for part in parsed_email.walk():
            content_type = part.get_content_type()
            content_disposition = str(part.get_content_disposition())
            # Look for 'text/html' content but ignore inline attachments.
            if content_type == 'text/html' and 'attachment' not in content_disposition:
                text_content = part.get_payload(decode=True)
                text_charset = part.get_content_charset()
                break
    else:
        text_content = parsed_email.get_payload(decode=True)
        text_charset = parsed_email.get_content_charset()

    if text_content and text_charset:
        return text_content.decode(text_charset)
    return