Fixed #35537 -- Changed EmailMessage.attachments and EmailMultiAlternatives.alternatives to use namedtuples.

This makes it more descriptive to pull out the named fields.
This commit is contained in:
Jake Howard 2024-06-09 09:09:07 +01:00 committed by Sarah Boyce
parent 9691a00d58
commit aba0e541ca
6 changed files with 86 additions and 16 deletions

View file

@ -133,7 +133,15 @@ Decorators
Email
~~~~~
* ...
* Tuple items of :class:`EmailMessage.attachments
<django.core.mail.EmailMessage>` and
:class:`EmailMultiAlternatives.attachments
<django.core.mail.EmailMultiAlternatives>` are now named tuples, as opposed
to regular tuples.
* :attr:`EmailMultiAlternatives.alternatives
<django.core.mail.EmailMultiAlternatives.alternatives>` is now a list of
named tuples, as opposed to regular tuples.
Error Reporting
~~~~~~~~~~~~~~~

View file

@ -282,8 +282,13 @@ All parameters are optional and can be set at any time prior to calling the
new connection is created when ``send()`` is called.
* ``attachments``: A list of attachments to put on the message. These can
be either :class:`~email.mime.base.MIMEBase` instances, or ``(filename,
content, mimetype)`` triples.
be either :class:`~email.mime.base.MIMEBase` instances, or a named tuple
with attributes ``(filename, content, mimetype)``.
.. versionchanged:: 5.2
In older versions, tuple items of ``attachments`` were regular tuples,
as opposed to named tuples.
* ``headers``: A dictionary of extra headers to put on the message. The
keys are the header name, values are the header values. It's up to the
@ -392,10 +397,10 @@ Django's email library, you can do this using the
.. class:: EmailMultiAlternatives
A subclass of :class:`~django.core.mail.EmailMessage` that has an
additional ``attach_alternative()`` method for including extra versions of
the message body in the email. All the other methods (including the class
initialization) are inherited directly from
A subclass of :class:`~django.core.mail.EmailMessage` that allows
additional versions of the message body in the email via the
``attach_alternative()`` method. This directly inherits all methods
(including the class initialization) from
:class:`~django.core.mail.EmailMessage`.
.. method:: attach_alternative(content, mimetype)
@ -415,6 +420,24 @@ Django's email library, you can do this using the
msg.attach_alternative(html_content, "text/html")
msg.send()
.. attribute:: alternatives
A list of named tuples with attributes ``(content, mimetype)``. This is
particularly useful in tests::
self.assertEqual(len(msg.alternatives), 1)
self.assertEqual(msg.alternatives[0].content, html_content)
self.assertEqual(msg.alternatives[0].mimetype, "text/html")
Alternatives should only be added using the
:meth:`~django.core.mail.EmailMultiAlternatives.attach_alternative`
method.
.. versionchanged:: 5.2
In older versions, ``alternatives`` was a list of regular tuples, as opposed
to named tuples.
Updating the default content type
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~