Ticket #12422: 12422-1.diff

File 12422-1.diff, 6.0 KB (added by claudep, 3 years ago)

Do not change python global CHARSETS

  • django/core/mail/message.py

    diff --git a/django/core/mail/message.py b/django/core/mail/message.py
    index 98ab3c9..5702b2e 100644
    a b from django.utils import six 
    2121
    2222# Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
    2323# some spam filters.
    24 Charset.add_charset('utf-8', Charset.SHORTEST, None, 'utf-8')
     24utf8_charset = Charset.Charset('utf-8')
     25utf8_charset.body_encoding = None  # Python defaults to BASE64
    2526
    2627# Default MIME type to use on attachments (if it is not explicitly given
    2728# and cannot be guessed).
    class SafeMIMEText(MIMEText): 
    123124
    124125    def __init__(self, text, subtype, charset):
    125126        self.encoding = charset
    126         MIMEText.__init__(self, text, subtype, charset)
     127        MIMEText.__init__(self, text, subtype, None)
     128        # Unfortunately, Python doesn't support setting a Charset instance as
     129        # MIMEText init parameter (http://bugs.python.org/issue16324)
     130        if charset == 'utf-8':
     131            self.set_charset(utf8_charset)
     132        else:
     133            self.set_charset(charset)
     134        self.replace_header('Content-Type', 'text/%s; charset="%s"' % (subtype, charset))
    127135
    128136    def __setitem__(self, name, val):
    129137        name, val = forbid_multi_line_headers(name, val, self.encoding)
  • tests/regressiontests/mail/tests.py

    diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py
    index 33898cc..dcc8b3a 100644
    a b class MailTests(TestCase): 
    8989        """
    9090        headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
    9191        email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers)
    92         self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent')
     92        self.assertEqual(email.message().as_string(), 'MIME-Version: 1.0\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent')
    9393
    9494    def test_from_header(self):
    9595        """
    class MailTests(TestCase): 
    171171        email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'from@example.com', ['other@example.com'])
    172172        email.encoding = 'iso-8859-1'
    173173        message = email.message()
    174         self.assertTrue(message.as_string().startswith('Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com'))
     174        self.assertTrue(message.as_string().startswith('MIME-Version: 1.0\nContent-Type: text/plain; charset="iso-8859-1"\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com'))
    175175        self.assertEqual(message.get_payload(), 'Firstname S=FCrname is a great guy.')
    176176
    177177        # Make sure MIME attachments also works correctly with other encodings than utf-8
    class MailTests(TestCase): 
    180180        msg = EmailMultiAlternatives('Subject', text_content, 'from@example.com', ['to@example.com'])
    181181        msg.encoding = 'iso-8859-1'
    182182        msg.attach_alternative(html_content, "text/html")
    183         self.assertEqual(msg.message().get_payload(0).as_string(), 'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.')
    184         self.assertEqual(msg.message().get_payload(1).as_string(), 'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>')
     183        self.assertEqual(msg.message().get_payload(0).as_string(), 'MIME-Version: 1.0\nContent-Type: text/plain; charset="iso-8859-1"\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.')
     184        self.assertEqual(msg.message().get_payload(1).as_string(), 'MIME-Version: 1.0\nContent-Type: text/html; charset="iso-8859-1"\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>')
    185185
    186186    def test_attachments(self):
    187187        """Regression test for #9367"""
    class BaseEmailBackendTests(object): 
    441441        email = EmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'], cc=['cc@example.com'])
    442442        mail.get_connection().send_messages([email])
    443443        message = self.get_the_message()
    444         self.assertStartsWith(message.as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ')
     444        self.assertStartsWith(message.as_string(), 'MIME-Version: 1.0\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nCc: cc@example.com\nDate: ')
    445445
    446446    def test_idn_send(self):
    447447        """
    class ConsoleBackendTests(BaseEmailBackendTests, TestCase): 
    589589        s = StringIO()
    590590        connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s)
    591591        send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
    592         self.assertTrue(s.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nDate: '))
     592        self.assertTrue(s.getvalue().startswith('MIME-Version: 1.0\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: from@example.com\nTo: to@example.com\nDate: '))
    593593
    594594
    595595class FakeSMTPServer(smtpd.SMTPServer, threading.Thread):
Back to Top