python / python/cpython

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

Open
#133,311 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-email type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Linked PRs
  • gh-133322

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the email.mime.image.MIMEImage and email.message.MIMEPart implementations, focusing on how each uses the supplied EmailPolicy during base64 encoding. Reproduce the example with max_line_length=35 and confirm that MIMEImage emits lines no longer than the policy allows; linked PR gh-133322 indicates work is already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.