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.
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>
* owner: nobody => CheesyPhoenix
* status: new => assigned
* stage: Unreviewed => Accepted
Comment:
Thanks for the report.
--
Ticket URL: <https://code.djangoproject.com/ticket/34904#comment:2>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34904#comment:3>
* 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>