def _prepare_message(txt):
 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 (not msg.has_key('Content-Transfer-Encoding')):
  encode_quopri(msg)
 return msg.as_string()

def _prepare_message ( txt ) : def plain ( val ) : 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 ( not msg . has_key ( 'Content-Transfer-Encoding' ) ) : encode_quopri ( msg ) return msg . as_string ( )
apply the proper encoding to all email fields .