#NEWLINE# def sanitize_email( addr , encoding = 'ascii' , parse = False ) : #NEWLINE# #INDENT# if parse : #NEWLINE# #INDENT# ( _ , addr ) = parseaddr ( to_unicode ( addr ) ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# addr . encode ( 'ascii' ) #NEWLINE# #UNINDENT# except UnicodeEncodeError : #NEWLINE# #INDENT# if ( '@' in parse ) : #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 addr