raw-unicode-escape and enc=nonebut returns single part with enc=none that is str instead of bytes in py if the entire header is unencoded (must handle mixed types here)see for more details/examplesthe following first attempt code was okay unless any encoded substringsor enc was returned as none (raised except which returned rawheader unchanged)hdrenc email header decode_header(rawheader)[ return hdr decode(encfails if enc=noneno encoding or encoded substrs ""tryparts email header decode_header(rawheaderdecoded [for (partencin partsfor all substrings if enc =nonepart unencodedif not isinstance(partbytes)strfull hdr unencoded decoded +[partelse do unicode decode elsedecoded +[part decode('raw-unicode-escape')elsedecoded +[part decode(enc)return join(decodedexceptreturn rawheader puntdef decodeaddrheader(selfrawheader)"" edecode existing address header text per email and unicodeaccording to its contentmust parse out first part of email address to get part'"=?utf- ? ?walmart?='from will probably have just addrbut toccbcc may have manydecodeheader handles nested encoded substrings within an entire hdrbut we can' simply call it for entire hdr here because it fails if encoded name substring ends in quote instead of whitespace or endstrsee also encodeaddrheader in mailsender module for the inverse of thisthe following first attempt code failed to handle encoded substrings in nameand raised exc for unencoded bytes parts if any encoded substringsnamebytesnameenc email header decode_header(name)[ (do email+mimeif nameencname namebytes decode(nameenc(do unicode?""trypairs email utils getaddresses([rawheader]split addrs and parts decoded [handles name commas for (nameaddrin pairstryname self decodeheader(nameemail+mime+uni exceptname none but uses encooded name if exc in decodeheader joined email utils formataddr((nameaddr)join parts decoded append(joinedreturn 'join(decoded> addrs exceptreturn self decodeheader(rawheadertry decoding entire string the mailtools utility package