def foldfix(msg): """ in_header = True buf = '' outmsg = '' for line in msg.split('\n'): if len(line) == 0: in_header=False if in_header: if line.endswith(':') and ' ' not in line: buf = line continue else: if buf: line = buf + line buf = '' outmsg += line + '\n' return outmsg