Author: kmtracey
Date: 2010-03-06 10:07:05 -0600 (Sat, 06 Mar 2010)
New Revision: 12689

Modified:
   django/branches/releases/1.1.X/
   django/branches/releases/1.1.X/django/core/mail.py
   django/branches/releases/1.1.X/tests/regressiontests/mail/tests.py
Log:
[1.1.X] Fixed #6918, #12791: If an email message has an encoding, actually use 
that encoding to encode body and headers. Thanks for patch with tests oyvind.

Backport of r12683 and r12688 from trunk.




Property changes on: django/branches/releases/1.1.X
___________________________________________________________________
Name: svnmerge-integrated
   - 
/django/trunk:1-11500,11523,11527-11528,11531-11552,11554,11577,11579-11581,11588-11589,11591-11592,11596-11599,11601-11617,11619-11626,11628-11635,11637-11638,11643-11644,11648-11653,11656,11670,11678,11681,11684,11686,11688,11691,11693,11695,11697,11699,11701,11703,11705,11707,11714,11719,11732,11734,11740,11748,11751,11753,11756,11760,11800,11802,11808,11815,11817,11820,11822,11824,11826,11828,11831,11833,11835,11837,11839,11841,11844,11857,11864,11874,11876,11878,11885,11898,11901,11905,11909,11912,11914,11917,11938,11953,11961,11977,11979,11984,11986,11988,11990,11992,11994,11996,11998,12001,12004,12006,12011,12022,12024,12044-12045,12048,12054-12056,12059,12064,12066,12068,12070,12079,12086,12088,12104,12118,12132,12137-12138,12140-12141,12144,12150-12152,12220-12221,12229,12249,12253,12276,12282,12284,12293,12313,12317-12324,12333,12341,12343,12346,12353,12362,12379,12384,12398,12405,12408-12411,12419-12420,12423,12425-12426,12429,12434,12436,12439-12442,12448,12457,12461-12464,12467,12473,12475,12490,12492,12497-12498,12502,12513,12515-12516,12518,12523,12526,12528,12533,12535,12537,12539,12541,12548,12553,12556,12558-12560,12562,12567,12569-12570,12573,12576,12581,12584,12616,12621-12622,12631,12648,12650,12652,12659,12661,12676,12679,12681
   + 
/django/trunk:1-11500,11523,11527-11528,11531-11552,11554,11577,11579-11581,11588-11589,11591-11592,11596-11599,11601-11617,11619-11626,11628-11635,11637-11638,11643-11644,11648-11653,11656,11670,11678,11681,11684,11686,11688,11691,11693,11695,11697,11699,11701,11703,11705,11707,11714,11719,11732,11734,11740,11748,11751,11753,11756,11760,11800,11802,11808,11815,11817,11820,11822,11824,11826,11828,11831,11833,11835,11837,11839,11841,11844,11857,11864,11874,11876,11878,11885,11898,11901,11905,11909,11912,11914,11917,11938,11953,11961,11977,11979,11984,11986,11988,11990,11992,11994,11996,11998,12001,12004,12006,12011,12022,12024,12044-12045,12048,12054-12056,12059,12064,12066,12068,12070,12079,12086,12088,12104,12118,12132,12137-12138,12140-12141,12144,12150-12152,12220-12221,12229,12249,12253,12276,12282,12284,12293,12313,12317-12324,12333,12341,12343,12346,12353,12362,12379,12384,12398,12405,12408-12411,12419-12420,12423,12425-12426,12429,12434,12436,12439-12442,12448,12457,12461-12464,12467,12473,12475,12490,12492,12497-12498,12502,12513,12515-12516,12518,12523,12526,12528,12533,12535,12537,12539,12541,12548,12553,12556,12558-12560,12562,12567,12569-12570,12573,12576,12581,12584,12616,12621-12622,12631,12648,12650,12652,12659,12661,12676,12679,12681,12683,12688


Modified: django/branches/releases/1.1.X/django/core/mail.py
===================================================================
--- django/branches/releases/1.1.X/django/core/mail.py  2010-03-06 15:50:12 UTC 
(rev 12688)
+++ django/branches/releases/1.1.X/django/core/mail.py  2010-03-06 16:07:05 UTC 
(rev 12689)
@@ -69,8 +69,9 @@
 class BadHeaderError(ValueError):
     pass
 
-def forbid_multi_line_headers(name, val):
+def forbid_multi_line_headers(name, val, encoding):
     """Forbids multi-line headers, to prevent header injection."""
+    encoding = encoding or settings.DEFAULT_CHARSET
     val = force_unicode(val)
     if '\n' in val or '\r' in val:
         raise BadHeaderError("Header values can't contain newlines (got %r for 
header %r)" % (val, name))
@@ -80,24 +81,34 @@
         if name.lower() in ('to', 'from', 'cc'):
             result = []
             for nm, addr in getaddresses((val,)):
-                nm = str(Header(nm, settings.DEFAULT_CHARSET))
+                nm = str(Header(nm.encode(encoding), encoding))
                 result.append(formataddr((nm, str(addr))))
             val = ', '.join(result)
         else:
-            val = Header(val, settings.DEFAULT_CHARSET)
+            val = Header(val.encode(encoding), encoding)
     else:
         if name.lower() == 'subject':
             val = Header(val)
     return name, val
 
 class SafeMIMEText(MIMEText):
+
+    def __init__(self, text, subtype, charset):
+        self.encoding = charset
+        MIMEText.__init__(self, text, subtype, charset)
+
     def __setitem__(self, name, val):
-        name, val = forbid_multi_line_headers(name, val)
+        name, val = forbid_multi_line_headers(name, val, self.encoding)
         MIMEText.__setitem__(self, name, val)
 
 class SafeMIMEMultipart(MIMEMultipart):
+
+    def __init__(self, _subtype='mixed', boundary=None, _subparts=None, 
encoding=None, **_params):
+        self.encoding = encoding
+        MIMEMultipart.__init__(self, _subtype, boundary, _subparts, **_params)
+
     def __setitem__(self, name, val):
-        name, val = forbid_multi_line_headers(name, val)
+        name, val = forbid_multi_line_headers(name, val, self.encoding)
         MIMEMultipart.__setitem__(self, name, val)
 
 class SMTPConnection(object):
@@ -234,7 +245,7 @@
 
     def message(self):
         encoding = self.encoding or settings.DEFAULT_CHARSET
-        msg = SafeMIMEText(smart_str(self.body, settings.DEFAULT_CHARSET),
+        msg = SafeMIMEText(smart_str(self.body, encoding),
                            self.content_subtype, encoding)
         msg = self._create_message(msg)
         msg['Subject'] = self.subject
@@ -293,8 +304,9 @@
 
     def _create_attachments(self, msg):
         if self.attachments:
+            encoding = self.encoding or settings.DEFAULT_CHARSET
             body_msg = msg
-            msg = SafeMIMEMultipart(_subtype=self.mixed_subtype)
+            msg = SafeMIMEMultipart(_subtype=self.mixed_subtype, 
encoding=encoding)
             if self.body:
                 msg.attach(body_msg)
             for attachment in self.attachments:
@@ -310,8 +322,8 @@
         """
         basetype, subtype = mimetype.split('/', 1)
         if basetype == 'text':
-            attachment = SafeMIMEText(smart_str(content,
-                settings.DEFAULT_CHARSET), subtype, settings.DEFAULT_CHARSET)
+            encoding = self.encoding or settings.DEFAULT_CHARSET
+            attachment = SafeMIMEText(smart_str(content, encoding), subtype, 
encoding)
         else:
             # Encode non-text attachments with base64.
             attachment = MIMEBase(basetype, subtype)
@@ -365,9 +377,10 @@
         return self._create_attachments(self._create_alternatives(msg))
 
     def _create_alternatives(self, msg):
+        encoding = self.encoding or settings.DEFAULT_CHARSET
         if self.alternatives:
             body_msg = msg
-            msg = SafeMIMEMultipart(_subtype=self.alternative_subtype)
+            msg = SafeMIMEMultipart(_subtype=self.alternative_subtype, 
encoding=encoding)
             if self.body:
                 msg.attach(body_msg)
             for alternative in self.alternatives:

Modified: django/branches/releases/1.1.X/tests/regressiontests/mail/tests.py
===================================================================
--- django/branches/releases/1.1.X/tests/regressiontests/mail/tests.py  
2010-03-06 15:50:12 UTC (rev 12688)
+++ django/branches/releases/1.1.X/tests/regressiontests/mail/tests.py  
2010-03-06 16:07:05 UTC (rev 12689)
@@ -106,10 +106,49 @@
 >>> email.message()['To']
 '=?utf-8?q?S=C3=BCrname=2C_Firstname?= <[email protected]>, [email protected]'
 
+# Regression for #6918 - When a header contains unicode,
+# make sure headers can be set with a different encoding than utf-8
+>>> email = EmailMessage('Message from Firstname Sürname', 'Content', 
'[email protected]', ['"Sürname, Firstname" 
<[email protected]>','[email protected]'])
+>>> email.encoding = 'iso-8859-1'
+>>> email.message()['To']
+'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <[email protected]>, [email protected]'
+>>> email.message()['Subject'].encode()
+u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?='
+
+# Make sure headers can be set with a different encoding than utf-8 in 
SafeMIMEMultipart as well
+>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
+>>> subject, from_email, to = 'hello', '[email protected]', '"Sürname, 
Firstname" <[email protected]>'
+>>> text_content = 'This is an important message.'
+>>> html_content = '<p>This is an <strong>important</strong> message.</p>'
+>>> msg = EmailMultiAlternatives('Message from Firstname Sürname', 
text_content, from_email, [to], headers=headers)
+>>> msg.attach_alternative(html_content, "text/html")
+>>> msg.encoding = 'iso-8859-1'
+>>> msg.message()['To']
+'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <[email protected]>'
+>>> msg.message()['Subject'].encode()
+u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?='
+
+# Regression for #12791  - Encode body correctly with other encodings than 
utf-8
+>>> email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 
'[email protected]', ['[email protected]'])
+>>> email.encoding = 'iso-8859-1'
+>>> message = email.message()
+>>> message.as_string()
+'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 
1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: 
[email protected]\nto: [email protected]\ndate: ...\nMessage-ID: 
<...>\n\nFirstname S=FCrname is a great guy.'
+
+# Make sure MIME attachments also works correctly with other encodings than 
utf-8
+>>> text_content = 'Firstname Sürname is a great guy.'
+>>> html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>'
+>>> msg = EmailMultiAlternatives('Subject', text_content, '[email protected]', 
['[email protected]'])
+>>> msg.encoding = 'iso-8859-1'
+>>> msg.attach_alternative(html_content, "text/html")
+>>> msg.message().get_payload(0).as_string()
+'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 
1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a 
great guy.'
+>>> msg.message().get_payload(1).as_string()
+'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 
1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a 
<strong>great</strong> guy.</p>'
+
 # 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.)
-
 >>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
 >>> subject, from_email, to = 'hello', '[email protected]', '[email protected]'
 >>> text_content = 'This is an important message.'

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to