Opened 21 months ago

Closed 6 days ago

#35713 closed Bug (fixed)

Django generates invalid address on unicode characters in the local part of an e-mail address

Reported by: Mike Edmunds Owned by:
Component: Core (Mail) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mike Edmunds)

#25986 attempted to add support for non-ASCII characters in the local-part (username) of an email address, by encoding it as an RFC 2047 encoded-word. PR#6377 landed in Django 1.10.

However, RFC 2047 specifically prohibits using an encoded-word in an addr-spec (the username@domain portion of an email address). Encoded-words are only allowed in address display-names.

The resulting email address is not supported by any known MTA or email client, and the message will either bounce or just disappear undelivered.

To reproduce:

from django.core.mail import EmailMessage
email = EmailMessage(to=["jörg@example.no"])
print(email.message().as_bytes().decode())  # examine generated message
# ...
# To: =?utf-8?b?asO2cmc=?=@example.no
# ...
email.send()  # if you've set up a mailbox for jörg at example.no

Actual results: as above (no errors)

Expected results: no =?utf-8?... encoded-word in the generated To addr-spec. And an error on the call to send() (or message()) saying that a non-ASCII local-part is not supported.

There is no supported way to send to non-ASCII usernames using 7-bit email headers. That requires using 8-bit headers with the SMTPUTF8 extension under RFC 6530/6531/6532. (That new feature request is ticket #35714; this ticket is solely about removing the current buggy behavior.)

For more details see #25986 comments 12-13.

Change History (8)

comment:1 by Claude Paroz, 21 months ago

Triage Stage: UnreviewedAccepted
Version: 5.0dev

Looks legitimate.

comment:2 by YashRaj1506, 21 months ago

Owner: set to YashRaj1506
Status: newassigned

comment:3 by Mike Edmunds, 21 months ago

Description: modified (diff)

comment:4 by YashRaj1506, 21 months ago

Owner: YashRaj1506 removed
Status: assignednew

comment:5 by Mike Edmunds, 21 months ago

The patch for #35581 raises an error for non-ASCII local-parts in send() when using the SMTP EmailBackend.

Catching the error earlier, in message(), would require additional work.

Last edited 6 days ago by Mike Edmunds (previous) (diff)

comment:6 by Vaibhav Pant, 20 months ago

Owner: set to Vaibhav Pant
Status: newassigned

comment:7 by Vaibhav Pant, 7 days ago

Owner: Vaibhav Pant removed
Status: assignednew

comment:8 by Mike Edmunds, 6 days ago

Resolution: fixed
Status: newclosed

This was fixed when sending through Django's SMTP EmailBackend via #35581.

The Python bug affecting EmailMessage.message() serialization in general was recently fixed upstream and will be in Python 3.15 final. (Though it won't be backported to earlier Python versions.)

I don't think there's anything more Django reasonably can or should do about this, so I'm marking this fixed.

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