#25986: RFC 6532 support for e-mail
---------------------------------+--------------------
     Reporter:  sergei-maertens  |      Owner:  nobody
         Type:  Bug              |     Status:  new
    Component:  Core (Mail)      |    Version:  1.9
     Severity:  Normal           |   Keywords:
 Triage Stage:  Unreviewed       |  Has patch:  0
Easy pickings:  0                |      UI/UX:  0
---------------------------------+--------------------
 With Python 3.5 and Django 1.9 I'm running into trouble with
 internationalized e-mail addresses. According to RFC 6532 it is possible
 to have unicode characters in the e-mail address:
 https://tools.ietf.org/html/rfc6532.html and this RFC *should* be
 supported in Python 3.5:
 https://docs.python.org/3/whatsnew/3.5.html#email. Steps to reproduce are
 at the bottom of the ticket.

 Now, for validating e-mail addresses in various places, Django calls
 stdlib `mail.formataddr`, which doesn't seem to respect this RFC - not
 even if you explicitly set the `EmailPolicy.utf8` (see
 
https://docs.python.org/3/library/email.policy.html#email.policy.EmailPolicy.utf8)
 as I see no reference to that in the source code.

 This function in the stdlib blatantly calls `address.encode('ascii')`.
 Luckily, it's quite short, and I would suggest rolling 'our own'
 `formataddr` function (for the time being). I'll bring this issue up on
 the Python bug tracker as well. I think it's possible, as it's a relative
 simple function of only 40 LoC, of which 12 lines docstring.


 **Steps to reproduce**
 Basically shell output:

 {{{
 #!bash
 mkdir bug_email
 cd bug_email
 mkdir bug_email -p python3.5
 (bug_email)➜  bug_email  python --version
 Python 3.5.1
 (bug_email)➜  bug_email  pip install Django==1.9
 Collecting Django==1.9
   Using cached Django-1.9-py2.py3-none-any.whl
 Installing collected packages: Django
 Successfully installed Django-1.9
 (bug_email)➜  bug_email  django-admin.py startproject bug_email .

 # regular shell is enough to test
 (bug_email)➜  bug_email  ./manage.py shell
 }}}

 Shell session
 {{{
 #!python
 >>> from django.core.mail.message import sanitize_address
 >>> sanitize_address(('dummy', u'juan.ló[email protected]'), 'utf8')
 Traceback (most recent call last):
   File "/home/bbt/coding/.virtualenvs/bug_email/lib/python3.5/site-
 packages/django/core/management/commands/shell.py", line 69, in handle
     self.run_shell(shell=options['interface'])
   File "/home/bbt/coding/.virtualenvs/bug_email/lib/python3.5/site-
 packages/django/core/management/commands/shell.py", line 61, in run_shell
     raise ImportError
 ImportError

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/bbt/coding/.virtualenvs/bug_email/lib/python3.5/site-
 packages/django/core/mail/message.py", line 118, in sanitize_address
     return formataddr((nm, addr))
   File "/usr/lib64/python3.5/email/utils.py", line 91, in formataddr
     address.encode('ascii')
 UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in
 position 6: ordinal not in range(128)
 >>>
 >>> sanitize_address(('dummy', u'juan.ló[email protected]'), 'idna')
 Traceback (most recent call last):
   File "/home/bbt/coding/.virtualenvs/bug_email/lib/python3.5/site-
 packages/django/core/management/commands/shell.py", line 69, in handle
     self.run_shell(shell=options['interface'])
   File "/home/bbt/coding/.virtualenvs/bug_email/lib/python3.5/site-
 packages/django/core/management/commands/shell.py", line 61, in run_shell
     raise ImportError
 ImportError

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/bbt/coding/.virtualenvs/bug_email/lib/python3.5/site-
 packages/django/core/mail/message.py", line 118, in sanitize_address
     return formataddr((nm, addr))
   File "/usr/lib64/python3.5/email/utils.py", line 91, in formataddr
     address.encode('ascii')
 UnicodeEncodeError: 'ascii' codec can't encode character '\xf3' in
 position 6: ordinal not in range(128)
 >>>
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25986>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/058.945d6c20680ae95e76cfa7625eb1ee80%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to