From 6e4c9840988b1aa3ae4598a7d04b13ada078be33 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 17 Aug 2012 20:16:38 +0200 Subject: [PATCH] [py3] Workarounded a Python bug in mail header encoding --- django/core/mail/message.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index b332ffba04..db9023a0bb 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -99,7 +99,12 @@ def sanitize_address(addr, encoding): if isinstance(addr, six.string_types): addr = parseaddr(force_text(addr)) nm, addr = addr - nm = Header(nm, encoding).encode() + # This try-except clause is needed on Python 3 < 3.2.4 + # http://bugs.python.org/issue14291 + try: + nm = Header(nm, encoding).encode() + except UnicodeEncodeError: + nm = Header(nm, 'utf-8').encode() try: addr.encode('ascii') except UnicodeEncodeError: # IDN