'Returns parent reference if embedded in In-Reply-To header
Message-ID is formatted as `{message_id}@{site}`'

def find_parent_from_in_reply_to(self, communication, email):

    parent = None
    in_reply_to = (email.mail.get(u'In-Reply-To') or u'').strip(u'    <>')
    if (in_reply_to and (u'@{0}'.format(frappe.local.site) in in_reply_to)):
        email_queue = frappe.db.get_value(u'Email    Queue', dict(message_id=in_reply_to), [u'communication', u'reference_doctype', u'reference_name'])
        if email_queue:
            (parent_communication, parent_doctype, parent_name) = email_queue
            if parent_communication:
                communication.in_reply_to = parent_communication
        else:
            (reference, domain) = in_reply_to.split(u'@', 1)
            (parent_doctype, parent_name) = (u'Communication', reference)
        if frappe.db.exists(parent_doctype, parent_name):
            parent = frappe._dict(doctype=parent_doctype, name=parent_name)
            if (parent_doctype == u'Communication'):
                if parent.reference_name:
                    parent = frappe.get_doc(parent.reference_doctype, parent.reference_name)
    return parent
