﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35361	SafeMIMEText.set_payload() crash on Python 3.11.9	Sarah Boyce	Natalia Bidart	"A recent bug fix to Python [https://github.com/python/cpython/issues/76511 gh-76511] has been back ported to Python 3.11 and 3.12.
See the release notes of [https://docs.python.org/release/3.11.9/whatsnew/changelog.html#python-3-11-9 Python 3.11.9] and the soon to be released [https://docs.python.org/3/whatsnew/changelog.html Python 3.12.3].

As part of the work to prepare Django for [https://code.djangoproject.com/ticket/34900 Python 3.13], a fix for this has already been merged to main in b231bcd19e57267ce1fc21d42d46f0b65fdcfcf8.

Believe this needs to be back ported to Django 4.2 and Django 5.0 to support the latest (or soon to be latest) Python 3.11 and Python 3.12 versions.

Confirmed when testing Django 4.2 against Python 3.11.9, we know have the following test failure:

{{{
======================================================================
ERROR [0.034s]: test_dont_base64_encode (tests.mail.tests.MailTests.test_dont_base64_encode)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/tests/django/tests/mail/tests.py"", line 902, in test_dont_base64_encode
    s = msg.message().as_string()
        ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ""/tests/django/django/core/mail/message.py"", line 133, in as_string
    g.flatten(self, unixfrom=unixfrom, linesep=linesep)
  File ""/usr/local/lib/python3.11/email/generator.py"", line 115, in flatten
    self._write(msg)
  File ""/usr/local/lib/python3.11/email/generator.py"", line 180, in _write
    self._dispatch(msg)
  File ""/usr/local/lib/python3.11/email/generator.py"", line 217, in _dispatch
    meth(msg)
  File ""/usr/local/lib/python3.11/email/generator.py"", line 246, in _handle_text
    msg.set_payload(msg._payload, charset)
  File ""/tests/django/django/core/mail/message.py"", line 168, in set_payload
    has_long_lines = any(
                     ^^^^
  File ""/tests/django/django/core/mail/message.py"", line 169, in <genexpr>
    len(line.encode()) > RFC5322_EMAIL_LINE_LENGTH_LIMIT
        ^^^^^^^^^^^^^
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 28-33: surrogates not allowed

}}}
"	Bug	closed	Core (Mail)	4.2	Release blocker	fixed			Accepted	1	0	0	0	0	0
