[Django] #34904: Changing email object after sending mutates mail in mail.outbox

2 views
Skip to first unread message

Django

unread,
Oct 18, 2023, 10:34:52 AM10/18/23
to django-...@googlegroups.com
#34904: Changing email object after sending mutates mail in mail.outbox
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
CheesyPhoenix |
Type: Bug | Status: new
Component: Core | Version: dev
(Mail) | Keywords: mail.outbox locmem
Severity: Normal | testing
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When testing emails using the locmem email backend with mail.outbox,
modifying an email object after calling `.send()` also modifies the email
object in `django.core.mail.outbox`. This leads to inconsistencies between
test and production environments, where an email modified in production
after calling `.send()` will not be changed since it has already been
sent.

Steps to reproduce:
- Run this test in any django project:
{{{#!python
def test_mutate_after_send(self) -> None:
email = EmailMessage(
subject="correct subject",
body="test body",
from_email="fr...@example.com",
to=["t...@example.com"],
)
email.send()
email.subject = "incorrect subject"
self.assertEqual("correct subject", mail.outbox[0].subject)
}}}

I have already implemented a fix on a fork and will open a PR soon.

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

Django

unread,
Oct 18, 2023, 10:47:09 AM10/18/23
to django-...@googlegroups.com
#34904: Changing email object after sending mutates mail in mail.outbox
-------------------------------------+-------------------------------------
Reporter: CheesyPhoenix | Owner: nobody
Type: Bug | Status: new
Component: Core (Mail) | Version: dev
Severity: Normal | Resolution:
Keywords: mail.outbox locmem | Triage Stage:
testing | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by CheesyPhoenix:

Old description:

> When testing emails using the locmem email backend with mail.outbox,
> modifying an email object after calling `.send()` also modifies the email
> object in `django.core.mail.outbox`. This leads to inconsistencies
> between test and production environments, where an email modified in
> production after calling `.send()` will not be changed since it has
> already been sent.
>
> Steps to reproduce:
> - Run this test in any django project:
> {{{#!python
> def test_mutate_after_send(self) -> None:
> email = EmailMessage(
> subject="correct subject",
> body="test body",
> from_email="fr...@example.com",
> to=["t...@example.com"],
> )
> email.send()
> email.subject = "incorrect subject"
> self.assertEqual("correct subject", mail.outbox[0].subject)
> }}}
>
> I have already implemented a fix on a fork and will open a PR soon.

New description:

When testing emails using the locmem email backend with mail.outbox,
modifying an email object after calling `.send()` also modifies the email
object in `django.core.mail.outbox`. This leads to inconsistencies between
test and production environments, where an email modified in production
after calling `.send()` will not be changed since it has already been
sent.

Steps to reproduce:
- Run this test in any django project:
{{{#!python
def test_mutate_after_send(self) -> None:
email = EmailMessage(
subject="correct subject",
body="test body",
from_email="fr...@example.com",
to=["t...@example.com"],
)
email.send()
email.subject = "incorrect subject"
self.assertEqual("correct subject", mail.outbox[0].subject)
}}}

GitHub PR fixing the issue: https://github.com/django/django/pull/17377

--

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

Django

unread,
Oct 19, 2023, 4:17:23 AM10/19/23
to django-...@googlegroups.com
#34904: Changing email object after sending mutates mail in mail.outbox
-------------------------------------+-------------------------------------
Reporter: CheesyPhoenix | Owner:
| CheesyPhoenix
Type: Bug | Status: assigned

Component: Core (Mail) | Version: dev
Severity: Normal | Resolution:
Keywords: mail.outbox locmem | Triage Stage: Accepted
testing |

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => CheesyPhoenix
* status: new => assigned
* stage: Unreviewed => Accepted


Comment:

Thanks for the report.

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

Django

unread,
Oct 25, 2023, 2:53:39 AM10/25/23
to django-...@googlegroups.com
#34904: Changing email object after sending mutates mail in mail.outbox
-------------------------------------+-------------------------------------
Reporter: CheesyPhoenix | Owner:
| CheesyPhoenix
Type: Bug | Status: assigned
Component: Core (Mail) | Version: dev
Severity: Normal | Resolution:
Keywords: mail.outbox locmem | Triage Stage: Ready for
testing | checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


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

Django

unread,
Oct 25, 2023, 3:34:49 AM10/25/23
to django-...@googlegroups.com
#34904: Changing email object after sending mutates mail in mail.outbox
-------------------------------------+-------------------------------------
Reporter: CheesyPhoenix | Owner:
| CheesyPhoenix
Type: Bug | Status: closed

Component: Core (Mail) | Version: dev
Severity: Normal | Resolution: fixed

Keywords: mail.outbox locmem | Triage Stage: Ready for
testing | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"64060d1c17e9d582b2b07cf866b1792267f07a79" 64060d1c]:
{{{
#!CommitTicketReference repository=""
revision="64060d1c17e9d582b2b07cf866b1792267f07a79"
Fixed #34904 -- Prevented mutating sent emails from outbox in locmem email
backend.
}}}

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

Reply all
Reply to author
Forward
0 new messages