Ticket #13433: bug13433.patch

File bug13433.patch, 7.6 KB (added by Leo Shklovskii, 15 years ago)

Patch against r13040

  • django/core/mail/message.py

     
    1313from django.core.mail.utils import DNS_NAME
    1414from django.utils.encoding import smart_str, force_unicode
    1515
     16try:
     17    from cStringIO import StringIO
     18except ImportError:
     19    from StringIO import StringIO
     20
    1621# Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
    1722# some spam filters.
    1823Charset.add_charset('utf-8', Charset.SHORTEST, Charset.QP, 'utf-8')
     
    8590    def __setitem__(self, name, val):   
    8691        name, val = forbid_multi_line_headers(name, val, self.encoding)
    8792        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.
    8898
     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
    89108class SafeMIMEMultipart(MIMEMultipart):
    90109   
    91110    def __init__(self, _subtype='mixed', boundary=None, _subparts=None, encoding=None, **_params):
     
    96115        name, val = forbid_multi_line_headers(name, val, self.encoding)
    97116        MIMEMultipart.__setitem__(self, name, val)
    98117
     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   
    99132class EmailMessage(object):
    100133    """
    101134    A container for email information.
  • tests/regressiontests/mail/tests.py

     
    11# coding: utf-8
    22
    33r"""
    4 # Tests for the django.core.mail.
    5 
     4# Tests for django.core.mail.
    65>>> import os
    76>>> import shutil
    87>>> import tempfile
     
    1615>>> from django.utils.translation import ugettext_lazy
    1716
    1817# Test normal ascii character case:
    19 
    2018>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'])
    2119>>> message = email.message()
    2220>>> message['Subject'].encode()
     
    2927'to@example.com'
    3028
    3129# Test multiple-recipient case
    32 
    3330>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com','other@example.com'])
    3431>>> message = email.message()
    3532>>> message['Subject'].encode()
     
    4239'to@example.com, other@example.com'
    4340
    4441# Test for header injection
    45 
    4642>>> email = EmailMessage('Subject\nInjection Test', 'Content', 'from@example.com', ['to@example.com'])
    4743>>> message = email.message()
    4844Traceback (most recent call last):
     
    5652BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection Test' for header 'Subject')
    5753
    5854# Test for space continuation character in long (ascii) subject headers (#7747)
    59 
    6055>>> 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'])
    6156>>> message = email.message()
    6257>>> message.as_string()
    6358'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'
    6459
    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.
    6862>>> headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
    6963>>> email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers)
    7064>>> email.message().as_string()
    7165'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'
    7266
    7367# Test that mail_admins/mail_managers doesn't connect to the mail server if there are no recipients (#9383)
    74 
    7568>>> old_admins = settings.ADMINS
    7669>>> old_managers = settings.MANAGERS
    7770>>> settings.ADMINS = []
     
    95881
    9689
    9790# Make sure we can manually set the From header (#9214)
    98 
    9991>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
    10092>>> message = email.message()
    10193>>> message['From']
     
    216208To: to@example.com
    217209Date: ...
    218210Message-ID: ...
    219 
     211<BLANKLINE>
    220212Content
    221213-------------------------------------------------------------------------------
    2222141
     
    235227To: to@example.com
    236228Date: ...
    237229Message-ID: ...
    238 
     230<BLANKLINE>
    239231Content
    240232-------------------------------------------------------------------------------
     233<BLANKLINE>
    241234
    242235# Make sure that dummy backends returns correct number of sent messages
    243236>>> connection = dummy.EmailBackend()
     
    288281To: to@example.com
    289282Date: ...
    290283Message-ID: ...
    291 
     284<BLANKLINE>
    292285Content
    293286-------------------------------------------------------------------------------
     287<BLANKLINE>
    294288
    295289>>> connection2 = filebased.EmailBackend(file_path=tmp_dir)
    296290>>> connection2.send_messages([email])
     
    355349To: to@example.com
    356350Date: ...
    357351Message-ID: ...
    358 
     352<BLANKLINE>
    359353Content
    360354-------------------------------------------------------------------------------
    3613551
     
    372366To: to1@example.com
    373367Date: ...
    374368Message-ID: ...
    375 
     369<BLANKLINE>
    376370Content1
    377371-------------------------------------------------------------------------------
    378372Content-Type: text/plain; charset="utf-8"
     
    383377To: to2@example.com
    384378Date: ...
    385379Message-ID: ...
    386 
     380<BLANKLINE>
    387381Content2
    388382-------------------------------------------------------------------------------
    3893832
     
    397391To: nobody@example.com
    398392Date: ...
    399393Message-ID: ...
    400 
     394<BLANKLINE>
    401395Content
    402396-------------------------------------------------------------------------------
    403397
     
    410404To: nobody@example.com
    411405Date: ...
    412406Message-ID: ...
    413 
     407<BLANKLINE>
    414408Content
    415409-------------------------------------------------------------------------------
    416410
    417411>>> settings.ADMINS = old_admins
    418412>>> settings.MANAGERS = old_managers
    419413
     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])
     419Content-Type: text/plain; charset="utf-8"
     420MIME-Version: 1.0
     421Content-Transfer-Encoding: quoted-printable
     422Subject: Subject
     423From: from@example.com
     424To: to@example.com
     425Date: ...
     426Message-ID: ...
     427<BLANKLINE>
     428From the future
     429-------------------------------------------------------------------------------
     4301
     431
    420432"""
Back to Top