def body_encode(body, maxlinelen=76, eol=NL):
 if (maxlinelen < 4):
  raise ValueError('maxlinelen must be at least 4')
 if (not body):
  return body
 body = body.translate(_QUOPRI_BODY_ENCODE_MAP)
 soft_break = ('=' + eol)
 maxlinelen1 = (maxlinelen - 1)
 encoded_body = []
 append = encoded_body.append
 for line in body.splitlines():
  start = 0
  laststart = ((len(line) - 1) - maxlinelen)
  while (start <= laststart):
   stop = (start + maxlinelen1)
   if (line[(stop - 2)] == '='):
    append(line[start:(stop - 1)])
    start = (stop - 2)
   elif (line[(stop - 1)] == '='):
    append(line[start:stop])
    start = (stop - 1)
   else:
    append((line[start:stop] + '='))
    start = stop
  if (line and (line[(-1)] in '  DCTB ')):
   room = (start - laststart)
   if (room >= 3):
    q = quote(line[(-1)])
   elif (room == 2):
    q = (line[(-1)] + soft_break)
   else:
    q = (soft_break + quote(line[(-1)]))
   append((line[start:(-1)] + q))
  else:
   append(line[start:])
 if (body[(-1)] in CRLF):
  append('')
 return eol.join(encoded_body)

def body_encode ( body , maxlinelen = 76 , eol = NL ) : if ( maxlinelen < 4 ) : raise ValueError ( 'maxlinelen must be at least 4' ) if ( not body ) : return body body = body . translate ( _QUOPRI_BODY_ENCODE_MAP ) soft_break = ( '=' + eol ) maxlinelen1 = ( maxlinelen - 1 ) encoded_body = [ ] append = encoded_body . append for line in body . splitlines ( ) : start = 0 laststart = ( ( len ( line ) - 1 ) - maxlinelen ) while ( start <= laststart ) : stop = ( start + maxlinelen1 ) if ( line [ ( stop - 2 ) ] == '=' ) : append ( line [ start : ( stop - 1 ) ] ) start = ( stop - 2 ) elif ( line [ ( stop - 1 ) ] == '=' ) : append ( line [ start : stop ] ) start = ( stop - 1 ) else : append ( ( line [ start : stop ] + '=' ) ) start = stop if ( line and ( line [ ( - 1 ) ] in '  DCTB ' ) ) : room = ( start - laststart ) if ( room >= 3 ) : q = quote ( line [ ( - 1 ) ] ) elif ( room == 2 ) : q = ( line [ ( - 1 ) ] + soft_break ) else : q = ( soft_break + quote ( line [ ( - 1 ) ] ) ) append ( ( line [ start : ( - 1 ) ] + q ) ) else : append ( line [ start : ] ) if ( body [ ( - 1 ) ] in CRLF ) : append ( '' ) return eol . join ( encoded_body )
encode a string with base64 .