[Django] #28726: Brackets illegal in DEFAULT_FROM_EMAIL name part

14 views
Skip to first unread message

Django

unread,
Oct 20, 2017, 4:16:44 AM10/20/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran | Owner: nobody
Courtney |
Type: Bug | Status: new
Component: Core | Version: 1.11
(Mail) | Keywords: email
Severity: Normal | DEFAULT_FROM_EMAIL
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Using `DEFAULT_FROM_EMAIL = '[test] Bob <nor...@example.com>'` will fail
in django.core.mail.message.sanitize_address() at
`parseaddr(force_text(addr))`

Possibly other characters are illegal. The docs don't mention the use of
DEFAULT_FROM_EMAIL in this way, perhaps documentation can cover it.

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

Django

unread,
Oct 25, 2017, 11:15:00 AM10/25/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: nobody
Type: Bug | Status: new
Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jani Sumak):

* stage: Unreviewed => Accepted


Old description:

> Using `DEFAULT_FROM_EMAIL = '[test] Bob <nor...@example.com>'` will fail
> in django.core.mail.message.sanitize_address() at
> `parseaddr(force_text(addr))`
>
> Possibly other characters are illegal. The docs don't mention the use of
> DEFAULT_FROM_EMAIL in this way, perhaps documentation can cover it.

New description:

Using `DEFAULT_FROM_EMAIL = '[test] Bob <nor...@example.com>'` will fail

in django.core.mail.message.sanitize_address() at `parseaddr(addr)`

Possibly other characters are illegal. The docs don't mention the use of
DEFAULT_FROM_EMAIL in this way, perhaps documentation can cover it.

--

Comment:

If `from_email` is not provided, `EmailMessage` or
`EmailMultiAlternatives` will use `DEFAULT_FROM_EMAIL`. If
DEFAULT_FROM_EMAIL is not a tuple `sanitize_address` will pass
DEFAULT_FROM_EMAIL to the function `parseaddr` from the `email` module in
the standard library. `parseaddr`will then try to parse RFC 2822
addresses.

Space and "(),:;<>@[\] characters are allowed with restrictions
([https://stackoverflow.com/a/2049510/4819353]). If you pass a suqare
bracket `[` to `parseaddr` it will return someting like this: `[('', ''),
('', 'test'), ('', ''), ('Bob', 'nor...@example.com')]`. Since
`sanitize_address`will use only the first element from the returned list,
you will get an error.

I suggest that the documentation mentions some of this.

{{{
DEFAULT_FROM_EMAIL¶
...

The value should be a tuple containing two strings, `(name, address)`, or
a string in the form of `name <em...@example.com>`.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:1>

Django

unread,
Oct 26, 2017, 12:20:42 PM10/26/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: nobody
Type: Bug | Status: new

Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jonatas CD):

Should also the default value be changed to a tuple?
Seems somewhat more coherent to me.
What do you think?

--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:2>

Django

unread,
Oct 26, 2017, 9:23:50 PM10/26/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: Rebeca
| Sarai
Type: Bug | Status: assigned

Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Rebeca Sarai):

* owner: nobody => Rebeca Sarai
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:3>

Django

unread,
Oct 26, 2017, 9:43:20 PM10/26/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: (none)
Type: Bug | Status: new

Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Rebeca Sarai):

* status: assigned => new
* owner: Rebeca Sarai => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:4>

Django

unread,
Oct 27, 2017, 11:19:15 AM10/27/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* type: Bug => Cleanup/optimization
* easy: 1 => 0


Comment:

Changing the default value to a tuple could be backwards incompatible.

I'm not sure if documentation is needed here, however, it could be helpful
to improve the exception:
{{{
File "/home/tim/code/django/tests/mail/tests.py", line 845, in
test_html_send_mail
send_mail('Subject', 'Content', '[test] Bob <nor...@example.com>',
['nob...@example.com'], html_message='HTML Content')
File "/home/tim/code/django/django/core/mail/__init__.py", line 62, in
send_mail
return mail.send()
File "/home/tim/code/django/django/core/mail/message.py", line 349, in
send
return self.get_connection(fail_silently).send_messages([self])
File "/home/tim/code/django/django/core/mail/backends/smtp.py", line
111, in send_messages
sent = self._send(message)
File "/home/tim/code/django/django/core/mail/backends/smtp.py", line
123, in _send
from_email = sanitize_address(email_message.from_email, encoding)
File "/home/tim/code/django/django/core/mail/message.py", line 161, in
sanitize_address
address = Address(nm, addr_spec=addr)
File "/home/tim/code/cpython/Lib/email/headerregistry.py", line 42, in
__init__
a_s, rest = parser.get_addr_spec(addr_spec)
File "/home/tim/code/cpython/Lib/email/_header_value_parser.py", line
1986, in get_addr_spec
token, value = get_local_part(value)
File "/home/tim/code/cpython/Lib/email/_header_value_parser.py", line
1798, in get_local_part
if value[0] in CFWS_LEADER:
IndexError: string index out of range
}}}
That might involve patching cpython rather than Django.

--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:3>

Django

unread,
Oct 30, 2017, 7:34:15 AM10/30/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam (Chainz) Johnson):

* cc: Adam (Chainz) Johnson (added)


Comment:

Maybe we can add a system check?

--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:4>

Django

unread,
Oct 30, 2017, 12:40:07 PM10/30/17
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

I also considered suggesting a system check but I doubt the issue is
common enough to justify it. The problem could also happen when using the
`from_email` argument of `EmailMessage` and improving the exception will
address that.

--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:5>

Django

unread,
Sep 26, 2019, 2:01:34 PM9/26/19
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part
-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: Glenn
Type: | Paquette
Cleanup/optimization | Status: assigned

Component: Core (Mail) | Version: 1.11
Severity: Normal | Resolution:
Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Glenn Paquette):

* status: new => assigned

* owner: nobody => Glenn Paquette


--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:6>

Django

unread,
Sep 30, 2019, 2:59:19 AM9/30/19
to django-...@googlegroups.com
#28726: Brackets illegal in DEFAULT_FROM_EMAIL name part.

-------------------------------------+-------------------------------------
Reporter: Ciaran Courtney | Owner: Glenn
Type: | Paquette
Cleanup/optimization | Status: closed
Component: Core (Mail) | Version: 3.0
Severity: Normal | Resolution: fixed

Keywords: email | Triage Stage: Accepted
DEFAULT_FROM_EMAIL |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: assigned => closed
* version: 1.11 => 3.0
* resolution: => fixed


Comment:

Changing `DEFAULT_FROM_EMAIL` documentation doesn't solve it because you
can always use `from_email` argument of `EmailMessage` (as Tim pointed
out).
Moreover this issue was solved in 2628ea95151feb68f43a2a740e6fb0799a94b14b
that improved an exception:
{{{
File "/django/django/core/mail/message.py", line 84, in sanitize_address
raise ValueError('Invalid address "%s"' % addr)
ValueError: Invalid address "[test] Bob <nor...@example.com>"
}}}

I don't think that changes in documentation are required.

--
Ticket URL: <https://code.djangoproject.com/ticket/28726#comment:7>

Reply all
Reply to author
Forward
0 new messages