Opened 2 years ago

Closed 2 years ago

#33660 closed Bug (needsinfo)

EmailMessage encode the subject in base64 when translated and contains non US-ASCII characters

Reported by: Yann Owned by: nobody
Component: Core (Mail) Version: 4.0
Severity: Normal Keywords: EmailMessage Translation base64 quoted-printable
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)...

Change History (1)

comment:1 by Mariusz Felisiak, 2 years ago

Resolution: needsinfo
Status: newclosed

Thanks for the report. However I cannot reproduce this issue with the following test:

  • tests/mail/tests.py

    diff --git a/tests/mail/tests.py b/tests/mail/tests.py
    index 183a0c0ab1..81921e9504 100644
    a b from django.core.mail.backends import console, dummy, filebased, locmem, smtp  
    2828from django.core.mail.message import BadHeaderError, sanitize_address
    2929from django.test import SimpleTestCase, override_settings
    3030from django.test.utils import requires_tz_support
    31 from django.utils.translation import gettext_lazy
     31from django.utils.translation import gettext_lazy, override
    3232from django.utils.version import PY311
    3333
    3434try:
    class BaseEmailBackendTests(HeadersCheckMixin):  
    12321232            message.get_payload(decode=True).decode(), "Je t'aime très fort"
    12331233        )
    12341234
     1235
     1236    def test_send_unicode_lazy(self):
     1237        email = EmailMessage(
     1238            gettext_lazy("Password reset"),
     1239            gettext_lazy("Password reset sent"),
     1240            "from@example.com",
     1241            ["to@example.com"],
     1242        )
     1243        with override("fr"):
     1244            num_sent = mail.get_connection().send_messages([email])
     1245            self.assertEqual(num_sent, 1)
     1246            message = self.get_the_message()
     1247            self.assertEqual(
     1248                message["subject"],
     1249                "=?utf-8?q?R=C3=A9initialisation_du_mot_de_passe?=",
     1250            )
     1251            self.assertEqual(
     1252                message.get_payload(decode=True).decode(),
     1253                "Message de réinitialisation du mot de passe envoyé",
     1254            )
     1255
    12351256    def test_send_long_lines(self):
    12361257        """
    12371258        Email line length is limited to 998 chars by the RFC:

Can you provide a sample project or a test case?

Note: See TracTickets for help on using tickets.
Back to Top