Opened 2 weeks ago

Last modified 2 days ago

#35378 assigned Bug

Incorrect folding of long address headers with special characters when using 7bit Content-Transfer-Encoding in EmailMessage

Reported by: andres Owned by: Lufafa Joshua
Component: Core (Mail) Version: dev
Severity: Normal Keywords:
Cc: Florian Apolloner, Joachim Jablon Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

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" <to@…>)
  • 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" <to@example.com>'

msg = EmailMessage(from_email='from@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 <to@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 <to@example.com>\n

More details here: https://github.com/anymail/django-anymail/issues/369

Change History (5)

comment:1 by Sarah Boyce, 2 weeks ago

Cc: Florian Apolloner Joachim Jablon added
Summary: Bad UTF-8 "To" header encoding in EmailMessageIncorrect folding of long address headers with special characters when using 7bit Content-Transfer-Encoding in EmailMessage
Triage Stage: UnreviewedAccepted
Version: 5.0dev

Thank you for the report!
I can replicate and the discussion in https://github.com/anymail/django-anymail/issues/369 is also very useful, thank you.

I'm pretty sure the problem only occurs with Python email's legacy email.message.Message, which uses a different folding algorithm (via email.policy.compat32) than modern email.message.EmailMessage (email.policy.default) uses.
My understanding is the Compat32 legacy layer is there specifically to replicate Python 2's email behavior (including any bugs), so there's not much point in reporting bugs against it.

Agree that #31784 is also somewhat related.

comment:2 by Lufafa Joshua, 9 days ago

Owner: changed from nobody to Lufafa Joshua
Status: newassigned

comment:3 by Lufafa Joshua, 4 days ago

Has patch: set
Last edited 4 days ago by Lufafa Joshua (previous) (diff)

comment:4 by andres, 4 days ago

Thanks, Lufafa Joshua! I manually reproduced your fix here, did a quick test and it seems to work!

comment:5 by Sarah Boyce, 2 days ago

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top