Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5887 -- Consolidated some duplicate code in SafeMIMEText and S…

…afeMIMEMultipart. Thanks, Carl Karsten

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6987 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1264bcf8b01086e3a39efe70632d60ff10db82b2 1 parent f633673
Adrian Holovaty authored January 02, 2008

Showing 1 changed file with 7 additions and 17 deletions. Show diff stats Hide diff stats

  1. 24  django/core/mail.py
24  django/core/mail.py
@@ -67,8 +67,7 @@ def make_msgid(idstring=None):
67 67
 class BadHeaderError(ValueError):
68 68
     pass
69 69
 
70  
-class SafeMIMEText(MIMEText):
71  
-    def __setitem__(self, name, val):
  70
+def forbid_multi_line_headers(name, val):
72 71
         "Forbids multi-line headers, to prevent header injection."
73 72
         if '\n' in val or '\r' in val:
74 73
             raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
@@ -84,25 +83,16 @@ def __setitem__(self, name, val):
84 83
                 val = ', '.join(result)
85 84
             else:
86 85
                 val = Header(force_unicode(val), settings.DEFAULT_CHARSET)
  86
+    return (name, val)
  87
+
  88
+class SafeMIMEText(MIMEText):
  89
+    def __setitem__(self, name, val):
  90
+        name, val = forbid_multi_line_headers(name, val)
87 91
         MIMEText.__setitem__(self, name, val)
88 92
 
89 93
 class SafeMIMEMultipart(MIMEMultipart):
90 94
     def __setitem__(self, name, val):
91  
-        "Forbids multi-line headers, to prevent header injection."
92  
-        if '\n' in val or '\r' in val:
93  
-            raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name)
94  
-        try:
95  
-            val = force_unicode(val).encode('ascii')
96  
-        except UnicodeEncodeError:
97  
-            if name.lower() in ('to', 'from', 'cc'):
98  
-                result = []
99  
-                for item in val.split(', '):
100  
-                    nm, addr = parseaddr(item)
101  
-                    nm = str(Header(nm, settings.DEFAULT_CHARSET))
102  
-                    result.append(formataddr((nm, str(addr))))
103  
-                val = ', '.join(result)
104  
-            else:
105  
-                val = Header(force_unicode(val), settings.DEFAULT_CHARSET)
  95
+        name, val = forbid_multi_line_headers(name, val)
106 96
         MIMEMultipart.__setitem__(self, name, val)
107 97
 
108 98
 class SMTPConnection(object):

0 notes on commit 1264bcf

Please sign in to comment.
Something went wrong with that request. Please try again.