ters@walmart com>pairs getaddresses([rawtoheader]pairs [('=?utf- ? ?walmart?=''newsletters@walmart com')('=?utf- ? ?walmart?=''ne wsletters@walmart com')addrs [for nameaddr in pairsabytesaenc decode_header(name)[ email+mime name abytes decode(aencunicode addrs append(formataddr((nameaddr))one or more addrs 'join(addrs'walmart walmart these tools are generally forgiving for unencoded content and return them intact to be robustthoughthe last portion of code here should also allow for multiple parts returned by decode_header (for encoded substrings)none encoding values for parts (for unencoded substrings)and str substring values instead of bytes (for fully unencoded namesdecoding this way applies both mime and unicode decoding steps to fetched mails creating properly encoded headers for inclusion in new mails composed and sent is similarly straightforwardfrom email header import make_header hdr make_header([( ' \xc \xe ''latin- ')]print(hdraabac print(hdr encode()=?iso- - ? ? = = ?decode_header(hdr encode()[( ' \xc \xe ''iso- - ')this can be applied to entire headers such as subjectas well as the name component of each email address in an address-related header line such as from and to (use getaddresses to split into individual addresses first if neededthe header object provides an alternative interfaceboth techniques handle additional detailssuch as line lengthsfor which we'll defer to python manualsfrom email header import header header( ' \xe \xc 'charset='latin- ' encode('=?iso- - ? ? = = ?= header('spam'charset='ascii'same as header('spam' encode('spamthe mailtools package ahead and its pymailgui client of will use these interfaces to automatically decode message headers in fetched mails per their content for displayand to encode headers sent that are not in ascii format that latter also emailparsing and composing mail content