Ticket #13433: bug13433.patch
File bug13433.patch, 7.6 KB (added by , 15 years ago) |
---|
-
django/core/mail/message.py
13 13 from django.core.mail.utils import DNS_NAME 14 14 from django.utils.encoding import smart_str, force_unicode 15 15 16 try: 17 from cStringIO import StringIO 18 except ImportError: 19 from StringIO import StringIO 20 16 21 # Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from 17 22 # some spam filters. 18 23 Charset.add_charset('utf-8', Charset.SHORTEST, Charset.QP, 'utf-8') … … 85 90 def __setitem__(self, name, val): 86 91 name, val = forbid_multi_line_headers(name, val, self.encoding) 87 92 MIMEText.__setitem__(self, name, val) 93 94 def as_string(self, unixfrom=False): 95 """Return the entire formatted message as a string. 96 Optional `unixfrom' when True, means include the Unix From_ envelope 97 header. 88 98 99 This overrides the default as_string() implementation to not mangle 100 lines that begin with 'From '. See bug #13433 for details. 101 """ 102 from email.generator import Generator 103 fp = StringIO() 104 g = Generator(fp, mangle_from_ = False) 105 g.flatten(self, unixfrom=unixfrom) 106 return fp.getvalue() 107 89 108 class SafeMIMEMultipart(MIMEMultipart): 90 109 91 110 def __init__(self, _subtype='mixed', boundary=None, _subparts=None, encoding=None, **_params): … … 96 115 name, val = forbid_multi_line_headers(name, val, self.encoding) 97 116 MIMEMultipart.__setitem__(self, name, val) 98 117 118 def as_string(self, unixfrom=False): 119 """Return the entire formatted message as a string. 120 Optional `unixfrom' when True, means include the Unix From_ envelope 121 header. 122 123 This overrides the default as_string() implementation to not mangle 124 lines that begin with 'From '. See bug #13433 for details. 125 """ 126 from email.generator import Generator 127 fp = StringIO() 128 g = Generator(fp, mangle_from_ = False) 129 g.flatten(self, unixfrom=unixfrom) 130 return fp.getvalue() 131 99 132 class EmailMessage(object): 100 133 """ 101 134 A container for email information. -
tests/regressiontests/mail/tests.py
1 1 # coding: utf-8 2 2 3 3 r""" 4 # Tests for the django.core.mail. 5 4 # Tests for django.core.mail. 6 5 >>> import os 7 6 >>> import shutil 8 7 >>> import tempfile … … 16 15 >>> from django.utils.translation import ugettext_lazy 17 16 18 17 # Test normal ascii character case: 19 20 18 >>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com']) 21 19 >>> message = email.message() 22 20 >>> message['Subject'].encode() … … 29 27 'to@example.com' 30 28 31 29 # Test multiple-recipient case 32 33 30 >>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com','other@example.com']) 34 31 >>> message = email.message() 35 32 >>> message['Subject'].encode() … … 42 39 'to@example.com, other@example.com' 43 40 44 41 # Test for header injection 45 46 42 >>> email = EmailMessage('Subject\nInjection Test', 'Content', 'from@example.com', ['to@example.com']) 47 43 >>> message = email.message() 48 44 Traceback (most recent call last): … … 56 52 BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection Test' for header 'Subject') 57 53 58 54 # Test for space continuation character in long (ascii) subject headers (#7747) 59 60 55 >>> email = EmailMessage('Long subject lines that get wrapped should use a space continuation character to get expected behaviour in Outlook and Thunderbird', 'Content', 'from@example.com', ['to@example.com']) 61 56 >>> message = email.message() 62 57 >>> message.as_string() 63 58 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Long subject lines that get wrapped should use a space continuation\n character to get expected behaviour in Outlook and Thunderbird\nFrom: from@example.com\nTo: to@example.com\nDate: ...\nMessage-ID: <...>\n\nContent' 64 59 65 # Specifying dates or message-ids in the extra headers overrides the defaul 66 # values (#9233). 67 60 # Regression for #9233 - Specifying dates or message-ids in the extra 61 # headers overrides the default values. 68 62 >>> headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} 69 63 >>> email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers) 70 64 >>> email.message().as_string() 71 65 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent' 72 66 73 67 # Test that mail_admins/mail_managers doesn't connect to the mail server if there are no recipients (#9383) 74 75 68 >>> old_admins = settings.ADMINS 76 69 >>> old_managers = settings.MANAGERS 77 70 >>> settings.ADMINS = [] … … 95 88 1 96 89 97 90 # Make sure we can manually set the From header (#9214) 98 99 91 >>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) 100 92 >>> message = email.message() 101 93 >>> message['From'] … … 216 208 To: to@example.com 217 209 Date: ... 218 210 Message-ID: ... 219 211 <BLANKLINE> 220 212 Content 221 213 ------------------------------------------------------------------------------- 222 214 1 … … 235 227 To: to@example.com 236 228 Date: ... 237 229 Message-ID: ... 238 230 <BLANKLINE> 239 231 Content 240 232 ------------------------------------------------------------------------------- 233 <BLANKLINE> 241 234 242 235 # Make sure that dummy backends returns correct number of sent messages 243 236 >>> connection = dummy.EmailBackend() … … 288 281 To: to@example.com 289 282 Date: ... 290 283 Message-ID: ... 291 284 <BLANKLINE> 292 285 Content 293 286 ------------------------------------------------------------------------------- 287 <BLANKLINE> 294 288 295 289 >>> connection2 = filebased.EmailBackend(file_path=tmp_dir) 296 290 >>> connection2.send_messages([email]) … … 355 349 To: to@example.com 356 350 Date: ... 357 351 Message-ID: ... 358 352 <BLANKLINE> 359 353 Content 360 354 ------------------------------------------------------------------------------- 361 355 1 … … 372 366 To: to1@example.com 373 367 Date: ... 374 368 Message-ID: ... 375 369 <BLANKLINE> 376 370 Content1 377 371 ------------------------------------------------------------------------------- 378 372 Content-Type: text/plain; charset="utf-8" … … 383 377 To: to2@example.com 384 378 Date: ... 385 379 Message-ID: ... 386 380 <BLANKLINE> 387 381 Content2 388 382 ------------------------------------------------------------------------------- 389 383 2 … … 397 391 To: nobody@example.com 398 392 Date: ... 399 393 Message-ID: ... 400 394 <BLANKLINE> 401 395 Content 402 396 ------------------------------------------------------------------------------- 403 397 … … 410 404 To: nobody@example.com 411 405 Date: ... 412 406 Message-ID: ... 413 407 <BLANKLINE> 414 408 Content 415 409 ------------------------------------------------------------------------------- 416 410 417 411 >>> settings.ADMINS = old_admins 418 412 >>> settings.MANAGERS = old_managers 419 413 414 # Regression for #13433 - Make sure that EmailMessage doesn't mangle 'From ' in 415 # message body. 416 >>> connection = console.EmailBackend() 417 >>> email = EmailMessage('Subject', 'From the future', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) 418 >>> connection.send_messages([email]) 419 Content-Type: text/plain; charset="utf-8" 420 MIME-Version: 1.0 421 Content-Transfer-Encoding: quoted-printable 422 Subject: Subject 423 From: from@example.com 424 To: to@example.com 425 Date: ... 426 Message-ID: ... 427 <BLANKLINE> 428 From the future 429 ------------------------------------------------------------------------------- 430 1 431 420 432 """