From 0d3ddcaf2c74638a32781f361d467af572ced95f Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Fri, 15 Mar 2024 12:51:40 +0100 Subject: [PATCH] [4.2.x] Refs #34900, Refs #35361 -- Fixed SafeMIMEText.set_payload() crash on Python 3.13. Payloads with surrogates are passed to the set_payload() since https://github.com/python/cpython/commit/f97f25ef5dfcdfec0d9a359fd970abd139cf3428 Backport of b231bcd19e57267ce1fc21d42d46f0b65fdcfcf8 from main. --- django/core/mail/message.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django/core/mail/message.py b/django/core/mail/message.py index f3fe6186c7..1d4c5271c8 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -166,7 +166,8 @@ class SafeMIMEText(MIMEMixin, MIMEText): def set_payload(self, payload, charset=None): if charset == "utf-8" and not isinstance(charset, Charset.Charset): has_long_lines = any( - len(line.encode()) > RFC5322_EMAIL_LINE_LENGTH_LIMIT + len(line.encode(errors="surrogateescape")) + > RFC5322_EMAIL_LINE_LENGTH_LIMIT for line in payload.splitlines() ) # Quoted-Printable encoding has the side effect of shortening long