Skip to content

email.mime.image.MIMEImage's policy argument doesn't take effect #133311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dkg opened this issue May 2, 2025 · 0 comments
Open

email.mime.image.MIMEImage's policy argument doesn't take effect #133311

dkg opened this issue May 2, 2025 · 0 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@dkg
Copy link

dkg commented May 2, 2025

Bug report

Bug description:

I'm comparing the creation of two MIME parts, one using MIMEImage and one using MIMEPart, with the same policy object, which asks for max_line_length=35.

the MIMEImage does not respect the max_line_length when base64-encoding, but the MIMEPart does:

#!/usr/bin/python3

from io import BytesIO
from email.policy import EmailPolicy
from email.mime.image import MIMEImage
from email.message import MIMEPart
from PIL import Image, ImageDraw

img = Image.new('RGBA', (20,20))
d = ImageDraw.Draw(img)
d.regular_polygon((10,6,6), 3, fill='red')
d.line([(16,10), (10,16)], fill='blue', width=2)
d.line([(16,16), (10,10)], fill='blue', width=2)
b = BytesIO()
img.save(b, format='png', optimize=True)
imgbytes = b.getvalue()

policy = EmailPolicy(max_line_length=35)

f = MIMEImage(imgbytes, policy=policy)
print(f)

g = MIMEPart(policy=policy)
g.set_content(imgbytes, 'image', 'png')
print(g)

The output is:

Content-Type: image/png
MIME-Version: 1.0
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAcElEQVR42uVTOxbAMAgS739nO3Tp
Rw20dqpbfARQEjOywiwYnCtkDKnbcLk66sqlT+zt9cidkE+6KwkZsgrzfcqVMpL2jo0447gYDpeA
rk+OnJHkIhAfTPRicihAf5YJrw7vjv0ZWRWM/ulivdPf1QZ2kDD9xppd8wAAAABJRU5ErkJggg==

Content-Type: image/png
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAABQAAAAU
CAYAAACNiR0NAAAAcElEQVR42uVTOxbA
MAgS739nO3TpRw20dqpbfARQEjOywiwY
nCtkDKnbcLk66sqlT+zt9cidkE+6KwkZ
sgrzfcqVMpL2jo0447gYDpeArk+OnJHk
IhAfTPRicihAf5YJrw7vjv0ZWRWM/uli
vdPf1QZ2kDD9xppd8wAAAABJRU5ErkJg
gg==

CPython versions tested on:

3.13

Operating systems tested on:

Linux

@dkg dkg added the type-bug An unexpected behavior, bug, or error label May 2, 2025
vedant713 added a commit to vedant713/cpython that referenced this issue May 2, 2025
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 python#133311: MIMEImage’s policy argument not taking effect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant