From 0be2afdec8175e69d43230862d91a238aac135b1 Mon Sep 17 00:00:00 2001 From: Youssef Badzi <95358038+badziyoussef@users.noreply.github.com> Date: Fri, 27 Dec 2024 12:38:49 +0100 Subject: [PATCH 1/2] add support of remote storage --- django_mailbox/models.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 4c76dc5..8ff1e39 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -719,24 +719,22 @@ class Message(models.Model): if encoding and encoding.lower() == 'quoted-printable': # Cannot use `email.encoders.encode_quopri due to # bug 14360: http://bugs.python.org/issue14360 - with open(attachment.document.path, 'rb') as f: - output = BytesIO() - encode_quopri( - BytesIO( - f.read() - ), - output, - quotetabs=True, - header=False, - ) - new.set_payload( - output.getvalue().decode().replace(' ', '=20') - ) + output = BytesIO() + encode_quopri( + BytesIO( + attachment.document.read() + ), + output, + quotetabs=True, + header=False, + ) + new.set_payload( + output.getvalue().decode().replace(' ', '=20') + ) del new['Content-Transfer-Encoding'] new['Content-Transfer-Encoding'] = 'quoted-printable' else: - with open(attachment.document.path, 'rb') as f: - new.set_payload(f.read()) + new.set_payload(attachment.document.read()) del new['Content-Transfer-Encoding'] encode_base64(new) except MessageAttachment.DoesNotExist: From cb9ed0bd21b923942e3a9b0e7cf0ac5c1543e15d Mon Sep 17 00:00:00 2001 From: Youssef Badzi <95358038+badziyoussef@users.noreply.github.com> Date: Mon, 26 May 2025 13:30:56 +0100 Subject: [PATCH 2/2] handle too many open files case --- django_mailbox/models.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 8ff1e39..33039e0 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -719,22 +719,24 @@ class Message(models.Model): if encoding and encoding.lower() == 'quoted-printable': # Cannot use `email.encoders.encode_quopri due to # bug 14360: http://bugs.python.org/issue14360 - output = BytesIO() - encode_quopri( - BytesIO( - attachment.document.read() - ), - output, - quotetabs=True, - header=False, - ) - new.set_payload( - output.getvalue().decode().replace(' ', '=20') - ) + with attachment.document.open('rb') as f: + output = BytesIO() + encode_quopri( + BytesIO( + f.read() + ), + output, + quotetabs=True, + header=False, + ) + new.set_payload( + output.getvalue().decode().replace(' ', '=20') + ) del new['Content-Transfer-Encoding'] new['Content-Transfer-Encoding'] = 'quoted-printable' else: - new.set_payload(attachment.document.read()) + with attachment.document.open('rb') as f: + new.set_payload(f.read()) del new['Content-Transfer-Encoding'] encode_base64(new) except MessageAttachment.DoesNotExist: