[Django] #33660: EmailMessage encode the subject in base64 when translated and contains non US-ASCII characters

4 views
Skip to first unread message

Django

unread,
Apr 24, 2022, 2:46:18 PM4/24/22
to django-...@googlegroups.com
#33660: EmailMessage encode the subject in base64 when translated and contains non
US-ASCII characters
-------------------------------------+-------------------------------------
Reporter: Yann | Owner: nobody
Type: Bug | Status: new
Component: Core | Version: 4.0
(Mail) | Keywords: EmailMessage
Severity: Normal | Translation base64 quoted-printable
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The usual behavior of the EmailMessage class regarding its subject is to
keep it as it is if i contains only US-ASCII characters and to encode it
as quoted-printable if it contains non US-ASCII characters.

That works well and avoid our emails to be considered as pishing by
antispam from email servers

But in the specific context of translations there is a bug -> if the
subject of an email is translated into another language (through the
built-in django translations tools) and contains non US-ASCII characters,
by an mechanism I didn't catch, the subject is encoded as base64 and not
quoted-printable.

This results in being considered as spam by the email servers (tested on
Gandi and Gmail)...

--
Ticket URL: <https://code.djangoproject.com/ticket/33660>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 25, 2022, 1:32:11 AM4/25/22
to django-...@googlegroups.com
#33660: EmailMessage encode the subject in base64 when translated and contains non
US-ASCII characters
-------------------------------------+-------------------------------------
Reporter: Yann | Owner: nobody
Type: Bug | Status: closed
Component: Core (Mail) | Version: 4.0
Severity: Normal | Resolution: needsinfo
Keywords: EmailMessage | Triage Stage:
Translation base64 quoted- | Unreviewed
printable |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => needsinfo


Comment:

Thanks for the report. However I cannot reproduce this issue with the
following test:
{{{#!diff
diff --git a/tests/mail/tests.py b/tests/mail/tests.py
index 183a0c0ab1..81921e9504 100644
--- a/tests/mail/tests.py
+++ b/tests/mail/tests.py
@@ -28,7 +28,7 @@ from django.core.mail.backends import console, dummy,
filebased, locmem, smtp
from django.core.mail.message import BadHeaderError, sanitize_address
from django.test import SimpleTestCase, override_settings
from django.test.utils import requires_tz_support
-from django.utils.translation import gettext_lazy
+from django.utils.translation import gettext_lazy, override
from django.utils.version import PY311

try:
@@ -1232,6 +1232,27 @@ class BaseEmailBackendTests(HeadersCheckMixin):
message.get_payload(decode=True).decode(), "Je t'aime très
fort"
)

+
+ def test_send_unicode_lazy(self):
+ email = EmailMessage(
+ gettext_lazy("Password reset"),
+ gettext_lazy("Password reset sent"),
+ "fr...@example.com",
+ ["t...@example.com"],
+ )
+ with override("fr"):
+ num_sent = mail.get_connection().send_messages([email])
+ self.assertEqual(num_sent, 1)
+ message = self.get_the_message()
+ self.assertEqual(
+ message["subject"],
+ "=?utf-8?q?R=C3=A9initialisation_du_mot_de_passe?=",
+ )
+ self.assertEqual(
+ message.get_payload(decode=True).decode(),
+ "Message de réinitialisation du mot de passe envoyé",
+ )
+
def test_send_long_lines(self):
"""
Email line length is limited to 998 chars by the RFC:
}}}

Can you provide a sample project or a test case?

--
Ticket URL: <https://code.djangoproject.com/ticket/33660#comment:1>

Reply all
Reply to author
Forward
0 new messages