address individuallygetaddresses ignores commas in names when spitting apart separate addressesand parseaddr doestoobecause it simply returns the first pair in the getaddresses result (some line breaks were added to the following for legibility)from email utils import getaddresses multi '"smithbobbob smith bob@bob com"bobgetaddresses([multi][('smithbob''bob@bob com')('bob smith''bob@bob com')('''bob@bob com')('bob''bob@bob com')[formataddr(pairfor pair in getaddresses([multi])['"smithbob''bob smith ''bob@bob com''bob ''join([formataddr(pairfor pair in getaddresses([multi])]'"smithbobbob smith bob@bob combob getaddresses(['bob@bob com']('''bob@bob com')handles single address cases too nowdecoding email addresses is really just an extra step before and after the normal header decoding logic we saw earlierrawfromheader '"=?utf- ? ?walmart?=from email utils import parseaddrformataddr from email header import decode_header nameaddr parseaddr(rawfromheadernameaddr ('=?utf- ? ?walmart?=''newsletters@walmart com'split into name/addr parts abytesaenc decode_header(name)[ abytesaenc ( 'walmart''utf- 'do email+mime decoding name abytes decode(aencname 'walmartdo unicode decoding formataddr((nameaddr)'walmart put parts back together although from headers will typically have just one addressto be fully robust we need to apply this to every address in headerssuch as toccand bcc againthe multiaddress getaddresses utility avoids comma clashes between names and address separatorssince it also handles the single address caseit suffices for from headers as wellrawfromheader '"=?utf- ? ?walmart?=rawtoheader rawfromheader 'rawfromheader rawtoheader client-side scripting