Ticket #29830: patch.diff

File patch.diff, 2.7 KB (added by Jannik Schürg, 6 years ago)
  • django/core/mail/message.py

    diff --git a/django/core/mail/message.py b/django/core/mail/message.py
    index 9a0f0eba45..2f89ffbfc4 100644
    a b class SafeMIMEText(MIMEMixin, MIMEText):  
    170170        MIMEText.__setitem__(self, name, val)
    171171
    172172    def set_payload(self, payload, charset=None):
    173         if charset == 'utf-8':
     173        if charset == 'utf-8' and not isinstance(charset, Charset.Charset):
    174174            has_long_lines = any(
    175175                len(l.encode()) > RFC5322_EMAIL_LINE_LENGTH_LIMIT
    176176                for l in payload.splitlines()
  • tests/mail/tests.py

    diff --git a/tests/mail/tests.py b/tests/mail/tests.py
    index b2de5e4c10..037963453c 100644
    a b import socket  
    88import sys
    99import tempfile
    1010import threading
    11 from email import message_from_binary_file, message_from_bytes
     11from email import message_from_binary_file, message_from_bytes, charset
    1212from email.header import Header
    1313from email.mime.text import MIMEText
    1414from email.utils import parseaddr
    class MailTests(HeadersCheckMixin, SimpleTestCase):  
    686686        # The child message header is not base64 encoded
    687687        self.assertIn('Child Subject', parent_s)
    688688
     689    def test_respect_body_encoding(self):
     690        """Test that a custom body encoding is respected."""
     691        body = 'Body with latin characters: àáä.'
     692        msg = EmailMessage(
     693            'Subject', body, 'bounce@example.com', ['to@example.com'],
     694            headers={'From': 'from@example.com'},
     695        )
     696        encoding = charset.Charset("utf-8")
     697        encoding.body_encoding = charset.QP
     698        msg.encoding = encoding
     699        message = msg.message()
     700        self.assertMessageHasHeaders(message, {
     701            ('MIME-Version', '1.0'),
     702            ('Content-Type', 'text/plain; charset="utf-8"'),
     703            ('Content-Transfer-Encoding', 'quoted-printable'),
     704        })
     705        self.assertEqual(message.get_payload(), encoding.body_encode(body))
     706
     707        body = 'Hello World'
     708        msg = EmailMessage(
     709            'Subject', body, 'bounce@example.com', ['to@example.com'],
     710            headers={'From': 'from@example.com'},
     711        )
     712        encoding = charset.Charset("iso-8859-1")
     713        encoding.body_encoding = charset.BASE64
     714        msg.encoding = encoding
     715        message = msg.message()
     716        self.assertMessageHasHeaders(message, {
     717            ('MIME-Version', '1.0'),
     718            ('Content-Type', 'text/plain; charset="iso-8859-1"'),
     719            ('Content-Transfer-Encoding', 'base64'),
     720        })
     721        self.assertEqual(message.get_payload(), encoding.body_encode(body))
     722
    689723    def test_sanitize_address(self):
    690724        """
    691725        Email addresses are properly sanitized.
Back to Top