From 9b8d3df7d008e69ea9a73c76b319b09091c06156 Mon Sep 17 00:00:00 2001 From: Vedant Ravindra Dhoke <66007382+vedant713@users.noreply.github.com> Date: Fri, 2 May 2025 19:16:54 -0400 Subject: [PATCH 1/4] mime: have MIMEImage respect policy.max_line_length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch updates Lib/email/mime/image.py so that MIMEImage honors the max_line_length setting from the provided EmailPolicy when Base64-encoding image payloads. - Adds imports for base64 and textwrap.wrap - Replaces the old `set_payload(_imagedata)` + `_encoder(self)` calls with an explicit Base64 encode, wrap to `policy.max_line_length` (default 76), and then calls `set_payload` with the wrapped text. - Ensures CRLF line endings and proper headers are still applied by set_payload. Fixes issue #133311: MIMEImage’s policy argument not taking effect. --- Lib/email/mime/image.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Lib/email/mime/image.py b/Lib/email/mime/image.py index dab9685848172b..6603fb68c52866 100644 --- a/Lib/email/mime/image.py +++ b/Lib/email/mime/image.py @@ -8,6 +8,8 @@ from email import encoders from email.mime.nonmultipart import MIMENonMultipart +import base64 +from textwrap import wrap class MIMEImage(MIMENonMultipart): @@ -39,9 +41,19 @@ def __init__(self, _imagedata, _subtype=None, raise TypeError('Could not guess image MIME subtype') MIMENonMultipart.__init__(self, 'image', _subtype, policy=policy, **_params) - self.set_payload(_imagedata) - _encoder(self) - + #self.set_payload(_imagedata) + #_encoder(self) + # 1) Turn your image bytes into one long Base64 string + b64str = base64.b64encode(_imagedata).decode('ascii') + + # 2) Ask the policy “how long should each line be?” + maxlen = getattr(self.policy, 'max_line_length', 76) + + # 3) Break that long string into chunks of that length + wrapped = "\r\n".join(wrap(b64str, maxlen)) + + # 4) Give the email library those nice, wrapped lines + self.set_payload(wrapped, 'base64', policy=self.policy) _rules = [] From d309c31d4c1e20f106c02f2ca45c6ff4dfd50f4c Mon Sep 17 00:00:00 2001 From: Vedant Ravindra Dhoke <66007382+vedant713@users.noreply.github.com> Date: Fri, 2 May 2025 19:27:16 -0400 Subject: [PATCH 2/4] bpo-133311: have MIMEImage respect policy.max_line_length This patch updates Lib/email/mime/image.py so that MIMEImage honors the max_line_length setting from the provided EmailPolicy when Base64-encoding image payloads. - Adds imports for base64 and textwrap.wrap - Replaces the old `self.set_payload(_imagedata)` + `_encoder(self)` calls with an explicit Base64 encode, wrap to `policy.max_line_length` (default 76), and then calls `self.set_payload` with the wrapped text. - Ensures CRLF line endings and proper headers are still applied by set_payload. From 9e6d97fc6ea5e31a35f04b2809c3c57a1964ebb8 Mon Sep 17 00:00:00 2001 From: Vedant Ravindra Dhoke <66007382+vedant713@users.noreply.github.com> Date: Fri, 2 May 2025 19:32:06 -0400 Subject: [PATCH 3/4] bpo-133311: add NEWS entry --- Misc/NEWS.d/next/133311.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/133311.rst diff --git a/Misc/NEWS.d/next/133311.rst b/Misc/NEWS.d/next/133311.rst new file mode 100644 index 00000000000000..99fd0d67dae19e --- /dev/null +++ b/Misc/NEWS.d/next/133311.rst @@ -0,0 +1 @@ +Fix email.mime.image.MIMEImage to respect policy.max_line_length (bpo-133311). From c10626b514f00d91f0ed63d079f7a6cdb48c6a5c Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 2 May 2025 23:35:27 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-05-02-23-35-26.gh-issue-133311.c36IjF.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-05-02-23-35-26.gh-issue-133311.c36IjF.rst diff --git a/Misc/NEWS.d/next/Library/2025-05-02-23-35-26.gh-issue-133311.c36IjF.rst b/Misc/NEWS.d/next/Library/2025-05-02-23-35-26.gh-issue-133311.c36IjF.rst new file mode 100644 index 00000000000000..fab58aa4754e6f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-02-23-35-26.gh-issue-133311.c36IjF.rst @@ -0,0 +1 @@ +Fix email.mime.image.MIMEImage to respect policy.max_line_length when Base64-encoding image payloads.