Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13433 -- Changed default behavior of Django email message wrap…

…pers to not mangle lines starting with 'From '. Thanks Leo for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15669 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 70d3c84d186d11edc2f6c7d46dd1d6ef26b4d56f 1 parent c5a6bad
Ramiro Morales authored February 28, 2011
35  django/core/mail/message.py
@@ -3,16 +3,21 @@
3 3
 import random
4 4
 import time
5 5
 from email import Charset, Encoders
  6
+from email.generator import Generator
6 7
 from email.MIMEText import MIMEText
7 8
 from email.MIMEMultipart import MIMEMultipart
8 9
 from email.MIMEBase import MIMEBase
9 10
 from email.Header import Header
10  
-from email.Utils import formatdate, getaddresses, formataddr
  11
+from email.Utils import formatdate, getaddresses, formataddr, parseaddr
11 12
 
12 13
 from django.conf import settings
13 14
 from django.core.mail.utils import DNS_NAME
14 15
 from django.utils.encoding import smart_str, force_unicode
15  
-from email.Utils import parseaddr
  16
+
  17
+try:
  18
+    from cStringIO import StringIO
  19
+except ImportError:
  20
+    from StringIO import StringIO
16 21
 
17 22
 # Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
18 23
 # some spam filters.
@@ -119,6 +124,19 @@ def __setitem__(self, name, val):
119 124
         name, val = forbid_multi_line_headers(name, val, self.encoding)
120 125
         MIMEText.__setitem__(self, name, val)
121 126
 
  127
+    def as_string(self, unixfrom=False):
  128
+        """Return the entire formatted message as a string.
  129
+        Optional `unixfrom' when True, means include the Unix From_ envelope
  130
+        header.
  131
+
  132
+        This overrides the default as_string() implementation to not mangle
  133
+        lines that begin with 'From '. See bug #13433 for details.
  134
+        """
  135
+        fp = StringIO()
  136
+        g = Generator(fp, mangle_from_ = False)
  137
+        g.flatten(self, unixfrom=unixfrom)
  138
+        return fp.getvalue()
  139
+
122 140
 
123 141
 class SafeMIMEMultipart(MIMEMultipart):
124 142
 
@@ -130,6 +148,19 @@ def __setitem__(self, name, val):
130 148
         name, val = forbid_multi_line_headers(name, val, self.encoding)
131 149
         MIMEMultipart.__setitem__(self, name, val)
132 150
 
  151
+    def as_string(self, unixfrom=False):
  152
+        """Return the entire formatted message as a string.
  153
+        Optional `unixfrom' when True, means include the Unix From_ envelope
  154
+        header.
  155
+
  156
+        This overrides the default as_string() implementation to not mangle
  157
+        lines that begin with 'From '. See bug #13433 for details.
  158
+        """
  159
+        fp = StringIO()
  160
+        g = Generator(fp, mangle_from_ = False)
  161
+        g.flatten(self, unixfrom=unixfrom)
  162
+        return fp.getvalue()
  163
+
133 164
 
134 165
 class EmailMessage(object):
135 166
     """
10  tests/regressiontests/mail/tests.py
@@ -11,8 +11,8 @@
11 11
 
12 12
 from django.conf import settings
13 13
 from django.core import mail
14  
-from django.core.mail import EmailMessage, mail_admins, mail_managers, EmailMultiAlternatives
15  
-from django.core.mail import send_mail, send_mass_mail
  14
+from django.core.mail import (EmailMessage, mail_admins, mail_managers,
  15
+        EmailMultiAlternatives, send_mail, send_mass_mail)
16 16
 from django.core.mail.backends import console, dummy, locmem, filebased, smtp
17 17
 from django.core.mail.message import BadHeaderError
18 18
 from django.test import TestCase
@@ -282,6 +282,12 @@ def test_connection_arg(self):
282 282
         self.assertEqual(len(connection.test_outbox), 1)
283 283
         self.assertEqual(connection.test_outbox[0].subject, '[Django] Manager message')
284 284
 
  285
+    def test_dont_mangle_from_in_body(self):
  286
+        # Regression for #13433 - Make sure that EmailMessage doesn't mangle
  287
+        # 'From ' in message body.
  288
+        email = EmailMessage('Subject', 'From the future', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
  289
+        self.assertFalse('>From the future' in email.message().as_string())
  290
+
285 291
 
286 292
 class BaseEmailBackendTests(object):
287 293
     email_backend = None

0 notes on commit 70d3c84

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