GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing code.djangoproject.com — #35378 (Incorrect folding of long address headers with special characters when using 7bit Content-Transfer-Encoding in EmailMessage) – Django Issues#35378 closed Bug (invalid)Incorrect folding of long address headers with special characters when using 7bit Content-Transfer-Encoding in EmailMessage
Description
Hi!
I'm getting errors when trying to send email messages when the "to" header has non-ASCII chars.
Code example: import email from django.core.mail import EmailMessage recipient = '"A véry long name with non-ASCII char and, comma" <[email protected]>' msg = EmailMessage(from_email='[email protected]', to=[recipient]).message() msg.policy = email.policy.default.clone(cte_type='7bit') print(msg.as_bytes())
That prints a bytes string with:
It has an unprotected comma, and the message is rejected by ESPs.
More details here: https://github.com/anymail/django-anymail/issues/369 Change History (12)comment:1 by Sarah Boyce, 3 years ago
Thank you for the report!
Agree that #31784 is also somewhat related. comment:2 by Lufafa Joshua, 3 years ago
comment:3 by Lufafa Joshua, 3 years ago
comment:4 by andres, 3 years ago
Thanks, Lufafa Joshua! I manually reproduced your fix here, did a quick test and it seems to work! comment:5 by Sarah Boyce, 3 years ago
comment:6 by Lufafa Joshua, 3 years ago
comment:7 by Lufafa Joshua, 2 years ago
comment:8 by Mike Edmunds, 2 years ago
[I'm responsible for the example cited above. Setting an existing message's
I've come to the conclusion that any attempt to use modern email.policy.default to serialize an email.message.Message (which is constructed with legacy email.policy.compat32) is prone to errors, and probably just isn't supported by the Python email package. Additional information in https://github.com/anymail/django-anymail/issues/369#issuecomment-2184252097
I would suggest not trying to fix this particular issue within Django. Long term, there would be a lot of advantages to upgrading django.core.mail to use modern email.message.EmailMessage (and email.policy.default). But that's a separate issue, and I think needs to happen all at once; trying to mix legacy and modern email code seems problematic.
Here's an updated test, using only documented Python email package features (generator policy override). It has the same problem, but I think clarifies this is more of a Python email bug than a Django problem: import email.generator import email.policy import io import django.core.mail to = '"Người nhận a very very long, name" <[email protected]>' mime_message = django.core.mail.EmailMessage(to=[to]).message() fp = io.BytesIO() g = email.generator.BytesGenerator( fp, policy=email.policy.default.clone(cte_type="7bit"), ) g.flatten(mime_message) print(fp.getvalue().decode("ascii")) # [... other headers ...] # To: =?utf-8?b?TmfGsOG7nWkgbmjhuq1u?= a very very long, name <[email protected]> comment:9 by Mike Edmunds, 2 years ago
comment:10 by Mike Edmunds, 2 years ago
comment:11 by Sarah Boyce, 2 years ago
Thank you for the update Mike, updated the ticket accordingly
Note:
See TracTickets
for help on using tickets.
Download in other formats: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |