[Django] #35378: Bad UTF-8 "To" header encoding in EmailMessage

7 views
Skip to first unread message

Django

unread,
Apr 16, 2024, 10:49:03 AM4/16/24
to django-...@googlegroups.com
#35378: Bad UTF-8 "To" header encoding in EmailMessage
---------------------------------------+------------------------
Reporter: andres | Owner: nobody
Type: Bug | Status: new
Component: Core (Mail) | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+------------------------
Hi!

I'm getting errors when trying to send email messages when the "to" header
has non-ASCII chars.
The problem seems to happen when all these conditions are true at the same
time:

- the readable name part of the recipient is big (e.g. "A very long and
big name for this recipient" <t...@example.com>)
- there is at least one non-ASCII char
- there is at least one "special char" (only seems to happen with comma or
parenthesis)
- the special char isn't close to or between no-ASCII chars

Code example:
{{{
import email
from django.core.mail import EmailMessage

recipient = '"A véry long name with non-ASCII char and, comma"
<t...@example.com>'

msg = EmailMessage(from_email='fr...@example.com',
to=[recipient]).message()
msg.policy = email.policy.default.clone(cte_type='7bit')
print(msg.as_bytes())
}}}

That prints a bytes string with:

`\nTo: A =?utf-8?q?v=C3=A9ry?= long name with non-ASCII char and, comma\n
<t...@example.com>\n`

It has an unprotected comma, and the message is rejected by ESPs.
Using default policy fix this problem, but possibly causes other bad
consequences:

`\nTo: =?utf-8?q?A_v=C3=A9ry_long_name_with_non-
ASCII_char_and=2C_comma?=\n <t...@example.com>\n`

More details here: https://github.com/anymail/django-anymail/issues/369
--
Ticket URL: <https://code.djangoproject.com/ticket/35378>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages