From 56db6a36fc5500a425740c446c4cd623b58a70c1 Mon Sep 17 00:00:00 2001 From: John Whitlock Date: Fri, 4 Mar 2022 20:57:50 -0600 Subject: [PATCH] Store attachments as a list Store and load attachments as a list of tuples (name, data_stream), rather than a dict {name: data_stream}. This allows multiple attachments to have the same name (or None if filenames are omitted). --- emails/utils.py | 2 +- emails/views.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/emails/utils.py b/emails/utils.py index be6f22eac9..76ca19deba 100644 --- a/emails/utils.py +++ b/emails/utils.py @@ -134,7 +134,7 @@ def _add_body_to_message(msg, message_body): def _add_attachments_to_message(msg, attachments): # attach attachments - for actual_att_name, attachment in attachments.items(): + for actual_att_name, attachment in attachments: # Define the attachment part and encode it using MIMEApplication. attachment.seek(0) att = MIMEApplication(attachment.read()) diff --git a/emails/views.py b/emails/views.py index ec6bdbfe6d..ff2d9c3149 100644 --- a/emails/views.py +++ b/emails/views.py @@ -794,7 +794,7 @@ def _get_attachment(part): def _get_all_contents(email_message): text_content = None html_content = None - attachments = {} + attachments = [] if email_message.is_multipart(): for part in email_message.walk(): try: @@ -802,7 +802,7 @@ def _get_all_contents(email_message): att_name, att = ( _get_attachment(part) ) - attachments[att_name] = att + attachments.append((att_name, att)) continue if part.get_content_type() == 'text/plain': text_content = part.get_content()