diff options
| author | Jake Howard <git@theorangeone.net> | 2024-06-09 09:09:07 +0100 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-06-20 09:43:40 +0200 |
| commit | aba0e541caaa086f183197eaaca0ac20a730bbe4 (patch) | |
| tree | 7178521b84f461c95903cbbb5c03b76b9b39a86f /django | |
| parent | 9691a00d5839e6137a2716526277013af9ee97ff (diff) | |
Fixed #35537 -- Changed EmailMessage.attachments and EmailMultiAlternatives.alternatives to use namedtuples.
This makes it more descriptive to pull out the named fields.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/mail/message.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/django/core/mail/message.py b/django/core/mail/message.py index 205c680561..7eee5da8b8 100644 --- a/django/core/mail/message.py +++ b/django/core/mail/message.py @@ -1,4 +1,5 @@ import mimetypes +from collections import namedtuple from email import charset as Charset from email import encoders as Encoders from email import generator, message_from_string @@ -190,6 +191,10 @@ class SafeMIMEMultipart(MIMEMixin, MIMEMultipart): MIMEMultipart.__setitem__(self, name, val) +Alternative = namedtuple("Alternative", ["content", "mimetype"]) +EmailAttachment = namedtuple("Attachment", ["filename", "content", "mimetype"]) + + class EmailMessage: """A container for email information.""" @@ -338,7 +343,7 @@ class EmailMessage: # actually binary, read() raises a UnicodeDecodeError. mimetype = DEFAULT_ATTACHMENT_MIME_TYPE - self.attachments.append((filename, content, mimetype)) + self.attachments.append(EmailAttachment(filename, content, mimetype)) def attach_file(self, path, mimetype=None): """ @@ -471,13 +476,15 @@ class EmailMultiAlternatives(EmailMessage): cc, reply_to, ) - self.alternatives = alternatives or [] + self.alternatives = [ + Alternative(*alternative) for alternative in (alternatives or []) + ] def attach_alternative(self, content, mimetype): """Attach an alternative content representation.""" if content is None or mimetype is None: raise ValueError("Both content and mimetype must be provided.") - self.alternatives.append((content, mimetype)) + self.alternatives.append(Alternative(content, mimetype)) def _create_message(self, msg): return self._create_attachments(self._create_alternatives(msg)) @@ -492,5 +499,9 @@ class EmailMultiAlternatives(EmailMessage): if self.body: msg.attach(body_msg) for alternative in self.alternatives: - msg.attach(self._create_mime_attachment(*alternative)) + msg.attach( + self._create_mime_attachment( + alternative.content, alternative.mimetype + ) + ) return msg |
