'Perform some cleaning / postprocess in the body and attachments
extracted from the email. Note that this processing is specific to the
mail module, and should not contain security or generic html cleaning.
Indeed those aspects should be covered by the html_sanitize method
located in tools.'

def _message_extract_payload_postprocess(self, message, body, attachments):

    root = lxml.html.fromstring(body)
    postprocessed = False
    to_remove = []
    for node in root.iter():
        if (('o_mail_notification' in (node.get('class') or '')) or ('o_mail_notification' in (node.get('summary') or ''))):
            postprocessed = True
            if (node.getparent() is not None):
                to_remove.append(node)
        if ((node.tag == 'img') and node.get('src', '').startswith('cid:')):
            cid = node.get('src').split(':', 1)[1]
            related_attachment = [attach for attach in attachments if (attach[2] and (attach[2].get('cid') == cid))]
            if related_attachment:
                node.set('data-filename', related_attachment[0][0])
                postprocessed = True
    for node in to_remove:
        node.getparent().remove(node)
    if postprocessed:
        body = etree.tostring(root, pretty_print=False, encoding='UTF-8')
    return (body, attachments)
