join(abytes decode('raw-unicode-escapeif enc =none else encfor (abytesencin parts'man where did you get that assistant?we'll use logic similar to the last step here in the mailtools package aheadbut also retain str substrings intact without attempting to decode late-breaking newsas write this in mid- it seems possible that this mixed typenonpolymorphicand franklynon-pythonic api behavior may be addressed in future python release in response to rant posted on the python developers list by book author whose work you might be familiar withthere is presently vigorous discussion of the topic there among other ideas is proposal for bytes-like type which carries with it an explicit unicode encodingthis may make it possible to treat some text cases in more generic fashion while it' impossible to foresee the outcome of such proposalsit' good to see that the issues are being actively explored stay tuned to this book' website for further developments in the python library api and unicode stories message address header encodings and parsingand header creation one wrinkle pertaining to the prior sectionfor message headers that contain email addresses ( from)the name component of the name/address pair might be encoded this way as well because the email package' header parser expects encoded substrings to be followed by whitespace or the end of stringwe cannot ask it to decode complete address-related header--quotes around name components will fail to support such internationalized address headerswe must also parse out the first part of the email address and then decode first of allwe need to extract the name and address parts of an email address using email package toolsfrom email utils import parseaddrformataddr parseaddr('"smithbob' ('smithbob''bob@bob com'formataddr( '"smithbobsplit into name/addr pair unencoded addr parseaddr('bob smith 'unquoted name part ('bob smith''bob@bob com'formataddr(parseaddr('bob smith ')'bob smith parseaddr('bob@bob com'('''bob@bob com'formataddr(parseaddr('bob@bob com')'bob@bob comsimpleno name fields with multiple addresses ( toseparate individual addresses by commas since email names might embed commastooblindly splitting on commas to run each emailparsing and composing mail content