#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    5
    Rep Power
    0

    Question Multipart email returning 0 for "is_multipart()"


    I'm trying to parse a multi-part email but regardless of what I try, I cannot get my code to recognize any emails as multipart MIME messages.

    The code is basically:
    Code:
    fp = open('C:\Python22\email.txt')
    msg = email.message_from_file(fp)
    fp.close()
    
    for part in msg.walk():
        if part.is_multipart():
            print part.is_multipart()
        else:
            print part.is_multipart()
    As a result I cannot seem to do anything to extract the attached .tif file, or even strip off the headers using either the rfc822 or email modules.

    I'd love any help, I'm getting desparate.

    The email I tried last is simple text email with a .tif attachment:
    Received: by usamcms4.mc.usa.xerox.com id <01C3681F.57295FB0@usamcms4.mc.usa.xerox.com>; Thu, 21 Aug 2003 16:03:51 -0400Message-ID: <052106A55179D611B34300096BB02E3FCB30CE@usamcms4.mc.usa.xerox.com>From: "Swift, Ryan" <Ryan.Swift@usa.xerox.com>To: "Swift, Ryan" <Ryan.Swift@usa.xerox.com>Subject: Attach this!Date: Thu, 21 Aug 2003 16:03:49 -0400MIME-Version: 1.0Content-Type: multipart/mixed; boundary="----_=_NextPart_000_01C3681F.1A9B6F98"This message is in MIME format. Since your mail reader does not understandthis format, some or all of this message may not be legible.------_=_NextPart_000_01C3681F.1A9B6F98Content-Type: text/plain; charset="iso-8859-1"Test test 1 2 3-------------------------------------------------------------Ryan SwiftImage Science Engineering & Technologyryan.swift@usa.xerox.com(585) 442-6392MS 111-30N-------------------------------------------------------------- <<tifemail.tif>> ------_=_NextPart_000_01C3681F.1A9B6F98Content-Type: application/octet-stream; name="tifemail.tif"Content-Transfer-Encoding: base64Content-Disposition: attachment; filename="tifemail.tif"SUkqAAgAAAAQAP4ABAABAAAAAAAAAAABAwABAAAACgAAAAEBAwABAAAACgAAAAIBAwABAAAACAAAA AMBAwABAAAAAQAAAAYBAwABAAAAAQAAAA4BAgAPAAAAzgAAABEBBAABAAAAjgUAABUBAwABAAAAAQAAABYBAwAB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ABAgQFBgcICQoLAQABBQEBAQEBAQAAAAAAAAABAAIDBAUGBwgJCgsQAAEEAQMCBAIFBwYIB QMMMwEAAhEDBCESMQVBUWETInGBMgYUkaGxQiMkFVLBYjM0coLRQwclklPw4fFjczUWorKDJkSTVGRFwqN0NhfSVeJl8rOEw9N14/NGJ5SkhbSVxNTk9KW1xdXl9VZmdoaWprbG1ub2N0dXZ3eHl6e3x9fn9xEAAgIBAgQEAwQFBgcHBgU1AQACEQMhMRIEQVFhcSITBT KBkRShsUIjwVLR8DMkYuFygpJDUxVjczTxJQYWorKDByY1wtJEk1SjF2RFVTZ0ZeLys4TD03Xj80aUpIW0lcTU5PSltcXV5fVWZn aGlqa2xtbm9ic3R1dnd4eXp7fH/9oADAMBAAIRAxEAPwC82uutgqrGxtY2sraNGxuA2Nb7XbW/9pfzFP0T+6Ppfvf6/wDsKn7u4+h2/tfzf/dlS/zfpf6/+hKSn//ZADhCSU0EIQAAAAAAVQAAAAEBAAAADwBBAGQAbwBiAGUAIABQAGgAbwB0AG8AcwBoAG8AcAAAABMAQQBkAG8AYgBlACAAUABoAG8 AdABvAHMAaABvAHAAIAA2AC4AMAAAAAEAmKKRnpObiY2UpXiJqKVxiqemc4eipH2VpKN8laClp6h8kqimfZGoqHiVsat+laqkd42 Nm5qlmJ2Xn5WdqqRrlK2ebpW3r36OoJ56oautjp2KlqGgiZ+jqoaWrqt3lbGvfpeonw==------_=_NextPart_000_01C3681F.1A9B6F98--
    To my eyes it's clearly a multi-part MIME message with a .tif attachment, what am I missing?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0
    try this

    part.get_maintype() == 'multipart'

    -----------
    m.get_maintype(default=None)


    Returns m's main content type, a string 'maintype' taken from header Content-Type converted to lowercase. When m has no header Content-Type, get_maintype returns default.
    -----------
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'm just looking at your code and i have to say I'm a little unsure of what your doing with the if statment, even if multipart is false you still go and print the same data as if it were true, do yourself a favour and just print part.is_multipart()

    Code:
    fp = open('C:\Python22\email.txt')
    msg = email.message_from_file(fp)
    fp.close()
    
    for part in msg.walk():
        print part.is_multipart()
    You'll get the same result and use less code. less code == less for python to do == better preformance (in theory)

    Mark.
    Last edited by netytan; August 21st, 2003 at 07:28 PM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    5
    Rep Power
    0
    Originally posted by netytan
    I'm just looking at your code and i have to say I'm a little unsure of what your doing with the if statment, even if multipart is false you still go and print the same data as if it were true, do yourself a favour and just print part.is_multipart()

    Code:
    fp = open('C:\Python22\email.txt')
    msg = email.message_from_file(fp)
    fp.close()
    
    for part in msg.walk():
        print part.is_multipart()
    You'll get the same result and use less code. less code == less for python to do == better preformance (in theory)

    Mark.
    Actually, I had some other stuff in the if/else statements that I removed for clarity's sake when posting. Otherwise I'd agree.

IMN logo majestic logo threadwatch logo seochat tools logo