Refs #34900 -- Fixed SafeMIMEText.set_payload() crash on Python 3.13.#17979
Refs #34900 -- Fixed SafeMIMEText.set_payload() crash on Python 3.13.#17979felixxm merged 1 commit intodjango:mainfrom
Conversation
Payloads with surrogates are passed to the set_payload() since python/cpython@f97f25e
|
@hramezani Thanks for checking 👍 |
|
@felixxm can you cherry-pick this commit for |
|
@awais786 no, Scratch that, it was indeed backported 0d3ddca and should be part of the next 4.2.x release. |
Yes, we made an exception since this is a compatibility issue with supported versions of Python. |
|
Hi all 😄 Does anyone know if there's a workaround until 5.0.5 gets released ? |
The next release for both 4.2.12 and 5.0.5 is planned for May, 6th. If you need a workaround sooner than that, you can apply the diff from 2e6ae1e in your Django installation. |
|
Thanks @nessita ! For anyone else needing a workaround:
Neither of these were simple for us due to the way our containers are build. Less ideal, but much simpler to implement, was to monkeypatch Django. Given that there is a known fix released in two weeks, we assessed it was ok to do so. We monkeypatched from the from django.apps import AppConfig
class TheRelevantApp(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "therelevantapp"
def ready(self):
# This is a patch to fix this issue: https://github.com/django/django/pull/17979
# The fix will come out with Django 5.0.5 on May 6th, so we should remove the patch
# when we upgrade
import django
from email import charset as Charset
from email.mime.text import MIMEText
utf8_charset_qp = Charset.Charset("utf-8")
utf8_charset = Charset.Charset("utf-8")
RFC5322_EMAIL_LINE_LENGTH_LIMIT = 998
class SafeMIMEText(django.core.mail.message.SafeMIMEText):
def set_payload(self, payload, charset=None):
if charset == "utf-8" and not isinstance(charset, Charset.Charset):
has_long_lines = any(
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
# lines, if any (#22561).
charset = utf8_charset_qp if has_long_lines else utf8_charset
MIMEText.set_payload(self, payload, charset=charset)
django.core.mail.message.SafeMIMEText = SafeMIMETextPS: this will fail if you use mypy and |
Mail sending in the current version of django has a bug with some versions of python. 3.8 should be compatible. See django/django#17979 (comment)
* fix email sending bug Mail sending in the current version of django has a bug with some versions of python. 3.8 should be compatible. See django/django#17979 (comment) * fix wrong version * Update Dockerfile --------- Co-authored-by: Henrik Hørlück Berg <henrik@horluck.no>
Payloads with surrogates are passed to the
set_payload()since python/cpython@f97f25e.ticket-34900
Logs