Remove some images from a mail message

Discussion in 'Python' started by Jason Friedman, Apr 21, 2013.

  1. 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.

    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.

    I cannot figure out how to read the incoming message and extract the images.

    message_in = email.message_from_binary_file(open(file_name, "rb"))
    for part in message_in.walk():
    print("-" * 80)
    print("type: " + part.get_content_maintype())
    for key, value in part.items():
    print("key: " + key)
    print("value: " + value)

    -------------------------

    type: multipart
    key: Return-Path
    value: <>
    key: X-Original-To
    value: myuser@myhost

    ....

    key: Content-Type
    value: multipart/alternative;
    boundary="_000_A9E5330AAB8D0D4E8F9372F872EE8504010458F671hostden_"

    --_000_A9E5330AAB8D0D4E8F9372F872EE85040104591ECChostden_--

    --_010_A9E5330AAB8D0D4E8F9372F872EE85040104591ECChostden_
    Content-Type: image/png; name="image001.png"
    Content-Description: image001.png
    Content-Disposition: inline; filename="image001.png"; size=9257;
    creation-date="Mon, 15 Apr 2013 17:48:29 GMT";
    modification-date="Mon, 15 Apr 2013 17:48:29 GMT"
    Content-ID: <9801A60>
    Content-Transfer-Encoding: base64

    iVBORw0KGgoAAAANSUhEUgAAANcAAAAwCAYAAACCPO+PAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ
    bWFnZVJlYWR5ccllPAAAI8tJREFUeNrsXQlYVdUW/u+Fey+TCqgJTqA4i4rzjDM4pGaZU449yxAM
    Xy+bfY1WZpmZkpapZVpZmpmiKCqoOOGEGjmLI6AioMxcuO/f5+wLl0HFqVd59/ftz3sO+5yzzz7r

    ....

    ----------------------

    I'm guessing my image is in there, how do I get it out?
     
    Jason Friedman, Apr 21, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Sutter
    Replies:
    0
    Views:
    668
    John Sutter
    Jan 13, 2004
  2. =?Utf-8?B?TXJGZXo=?=

    Mail message size problem (System.Web.Mail)

    =?Utf-8?B?TXJGZXo=?=, Mar 14, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    669
    =?Utf-8?B?TXJGZXo=?=
    Mar 14, 2005
  3. radgey gadgey hl
    Replies:
    0
    Views:
    737
    radgey gadgey hl
    Nov 25, 2007
  4. Dave Angel
    Replies:
    0
    Views:
    143
    Dave Angel
    Apr 21, 2013
  5. Jason Friedman

    Re: Remove some images from a mail message

    Jason Friedman, Apr 23, 2013, in forum: Python
    Replies:
    0
    Views:
    118
    Jason Friedman
    Apr 23, 2013
Loading...

Share This Page