From dd5d7622a5d8f95c304267b1bcb0d9a8d28d6d8e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Wed, 4 Nov 2009 11:24:56 +0000 Subject: [PATCH] Fixed #11144 -- When a to/from/cc header contains unicode, make sure the email addresses are parsed correctly (especially with regards to commas). Thanks to rmt for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11719 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/mail/message.py | 5 ++--- tests/regressiontests/mail/tests.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index 7252a5a6205..14d0017311e 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -7,7 +7,7 @@ from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.Header import Header -from email.Utils import formatdate, parseaddr, formataddr +from email.Utils import formatdate, getaddresses, formataddr from django.conf import settings from django.core.mail.utils import DNS_NAME @@ -64,8 +64,7 @@ def forbid_multi_line_headers(name, val): except UnicodeEncodeError: if name.lower() in ('to', 'from', 'cc'): result = [] - for item in val.split(', '): - nm, addr = parseaddr(item) + for nm, addr in getaddresses((val,)): nm = str(Header(nm, settings.DEFAULT_CHARSET)) result.append(formataddr((nm, str(addr)))) val = ', '.join(result) diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index 5033581a090..10d2ae7df30 100644 --- a/tests/regressiontests/mail/tests.py +++ b/tests/regressiontests/mail/tests.py @@ -101,6 +101,17 @@ BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection T >>> message['From'] 'from@example.com' +# Regression for #11144 - When a to/from/cc header contains unicode, +# make sure the email addresses are parsed correctly (especially +# with regards to commas) +>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['"Firstname Sürname" ','other@example.com']) +>>> email.message()['To'] +'=?utf-8?q?Firstname_S=C3=BCrname?= , other@example.com' + +>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['"Sürname, Firstname" ','other@example.com']) +>>> email.message()['To'] +'=?utf-8?q?S=C3=BCrname=2C_Firstname?= , other@example.com' + # Handle attachments within an multipart/alternative mail correctly (#9367) # (test is not as precise/clear as it could be w.r.t. email tree structure, # but it's good enough.)