<div dir="ltr"><div>I will be receiving email that contains, say, 10 images, and I want to forward that message on after removing, say, 5 of those images. I will remove based on size, for example 1679 bytes. I am aware that other images besides the unwanted ones could be 1679 bytes but this is unlikely and the impact of mistakes is small.</div>
<div><br></div><div>I have figured out how to compose and send an email message from parts: from, to, subject, and some images. I was planning to read the incoming message, write the images I want to keep to files, then use those files to construct the outgoing message.</div>
<div><br></div><div>I cannot figure out how to read the incoming message and extract the images.</div><div><br></div><div>message_in = email.message_from_binary_file(open(file_name, "rb"))<br></div><div>
for part in message_in.walk():<br></div><div><div> print("-" * 80)</div><div> print("type: " + part.get_content_maintype())</div>
<div> for key, value in part.items():</div><div> print("key: " + key)</div><div> print("value: " + value)</div></div><div><br></div><div>-------------------------</div><div><br></div>
<div><div>type: multipart</div><div>key: Return-Path</div>
<div>value: <<a href="mailto:jfriedman@mycompany.com" target="_blank">jfriedman@mycompany.com</a>></div><div>key: X-Original-To</div><div>value: myuser@myhost</div><div><br></div><div>...</div><div><br></div><div>key: Content-Type</div>
<div>value: multipart/alternative;</div>
<div> boundary="_000_A9E5330AAB8D0D4E8F9372F872EE8504010458F671hostden_"</div></div><div><br></div><div><div>--_000_A9E5330AAB8D0D4E8F9372F872EE85040104591ECChostden_--</div><div><br></div><div>--_010_A9E5330AAB8D0D4E8F9372F872EE85040104591ECChostden_</div>
<div>Content-Type: image/png; name="image001.png"</div><div>Content-Description: image001.png</div><div>Content-Disposition: inline; filename="image001.png"; size=9257;</div><div> creation-date="Mon, 15 Apr 2013 17:48:29 GMT";</div>
<div> modification-date="Mon, 15 Apr 2013 17:48:29 GMT"</div><div>Content-ID: <image001.png@01CE39DF.E9801A60></div><div>Content-Transfer-Encoding: base64</div><div><br></div><div>iVBORw0KGgoAAAANSUhEUgAAANcAAAAwCAYAAACCPO+PAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ</div>
<div>bWFnZVJlYWR5ccllPAAAI8tJREFUeNrsXQlYVdUW/u+Fey+TCqgJTqA4i4rzjDM4pGaZU449yxAM</div><div>Xy+bfY1WZpmZkpapZVpZmpmiKCqoOOGEGjmLI6AioMxcuO/f5+wLl0HFqVd59/ftz3sO+5yzzz7r</div></div><div><br></div><div>...</div><div><br></div>
<div>----------------------<br></div><div><br></div><div style>I'm guessing my image is in there, how do I get it out?</div></div>