human
Complete the below
def _prepare_message(txt):

    ''''Apply the proper encoding to all email fields.
    The body will be Latin-1, the headers will be \'quopri\'d when necessary.'
    '''
assistant
#Here is the Python program you requested.
    def plain(val):
        '    Return    True    when    val    is    plain    ASCII    '
        try:
            val.decode('ascii')
            return True
        except:
            return False
    code = 'ISO-8859-1'
    msg = Message()
    payload = []
    body = False
    header = False
    for line in txt.encode(code, 'replace').split('\n'):
        if (header and (not line)):
            body = True
        if body:
            payload.append(line)
        else:
            m = RE_HEADER.search(line)
            if m:
                header = True
                keyword = m.group(1).strip()
                value = m.group(2).strip()
                if plain(value):
                    msg.add_header(keyword, value)
                else:
                    header = Header(value, code)
                    msg[keyword] = header
    msg.set_payload('\n'.join(payload), code)
    if ('Content-Transfer-Encoding' not in msg):
        encode_quopri(msg)
    return msg.as_string()