#NEWLINE# def sanitize_address( addr , encoding ) : #NEWLINE# #INDENT# if isinstance ( addr , six . string_types ) : #NEWLINE# #INDENT# addr = parseaddr ( force_text ( addr ) ) #NEWLINE# #UNINDENT# ( nm , addr ) = nm #NEWLINE# try : #NEWLINE# #INDENT# nm = Header ( nm , encoding ) . encode ( ) #NEWLINE# #UNINDENT# except UnicodeEncodeError : #NEWLINE# #INDENT# nm = Header ( nm , 'utf-8' ) . encode ( ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# addr . encode ( 'ascii' ) #NEWLINE# #UNINDENT# except UnicodeEncodeError : #NEWLINE# #INDENT# if ( '@' in addr ) : #NEWLINE# #INDENT# ( localpart , domain ) = addr . split ( '@' , 1 ) #NEWLINE# localpart = str ( Header ( localpart , encoding ) ) #NEWLINE# domain = domain . encode ( 'idna' ) . decode ( 'ascii' ) #NEWLINE# addr = '@' . join ( [ localpart , domain ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# addr = Header ( addr , encoding ) . encode ( ) #NEWLINE# #UNINDENT# #UNINDENT# return formataddr ( ( nm , addr ) )