﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25986	Django crashes on unicode characters in the local part of an e-mail address	Sergei Maertens	Sergei Maertens	"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ópez@abc.com'), '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ópez@abc.com'), '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)
>>> 
}}}"	Bug	closed	Core (Mail)	1.9	Normal	fixed		george@… martin.pajuste@…	Accepted	1	0	0	0	0	0
