Ticket #5778: django-mail-encoding-header-fix

File django-mail-encoding-header-fix, 2.7 KB (added by Thomas Petazzoni <thomas.petazzoni@…>, 7 years ago)

Ugly patch that fixes the problem for me

Line 
1Index: django/core/mail.py
2===================================================================
3--- django/core/mail.py (révision 6526)
4+++ django/core/mail.py (copie de travail)
5@@ -72,18 +72,15 @@
6         "Forbids multi-line headers, to prevent header injection."
7         if '\n' in val or '\r' in val:
8             raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
9-        try:
10-            val = str(force_unicode(val))
11-        except UnicodeEncodeError:
12-            if name.lower() in ('to', 'from', 'cc'):
13-                result = []
14-                for item in val.split(', '):
15-                    nm, addr = parseaddr(item)
16-                    nm = str(Header(nm, settings.DEFAULT_CHARSET))
17-                    result.append(formataddr((nm, str(addr))))
18-                val = ', '.join(result)
19-            else:
20-                val = Header(force_unicode(val), settings.DEFAULT_CHARSET)
21+        if name.lower() in ('to', 'from', 'cc'):
22+            result = []
23+            for item in val.split(', '):
24+                nm, addr = parseaddr(item)
25+                nm = str(Header(nm, settings.DEFAULT_CHARSET))
26+                result.append(formataddr((nm, str(addr))))
27+            val = ', '.join(result)
28+        else:
29+            val = Header(force_unicode(val), settings.DEFAULT_CHARSET)
30         MIMEText.__setitem__(self, name, val)
31 
32 class SafeMIMEMultipart(MIMEMultipart):
33@@ -91,18 +88,15 @@
34         "Forbids multi-line headers, to prevent header injection."
35         if '\n' in val or '\r' in val:
36             raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
37-        try:
38-            val = str(force_unicode(val))
39-        except UnicodeEncodeError:
40-            if name.lower() in ('to', 'from', 'cc'):
41-                result = []
42-                for item in val.split(', '):
43-                    nm, addr = parseaddr(item)
44-                    nm = str(Header(nm, settings.DEFAULT_CHARSET))
45-                    result.append(formataddr((nm, str(addr))))
46-                val = ', '.join(result)
47-            else:
48-                val = Header(force_unicode(val), settings.DEFAULT_CHARSET)
49+        if name.lower() in ('to', 'from', 'cc'):
50+            result = []
51+            for item in val.split(', '):
52+                nm, addr = parseaddr(item)
53+                nm = str(Header(nm, settings.DEFAULT_CHARSET))
54+                result.append(formataddr((nm, str(addr))))
55+            val = ', '.join(result)
56+        else:
57+            val = Header(force_unicode(val), settings.DEFAULT_CHARSET)
58         MIMEMultipart.__setitem__(self, name, val)
59 
60 class SMTPConnection(object):
Back to Top