walking a MIME encoded multipart email [python]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Stockwell

    walking a MIME encoded multipart email [python]

    snippet

    payload = {}
    try:
    message = email.message_f rom_string(mess age)
    messageHeader = dict(message._h eaders)

    addressFrom = messageHeader["From"]
    addressReply = messageHeader.g et("Reply-To")
    messageId = messageHeader.g et("Message-ID")

    if messageId is not None:
    if addressFrom is not None:
    if addressReply is not None:
    if message.is_mult ipart:
    count = 0
    for partition in message.walk():
    if partition.get_t ype() == "applicatio n/vnd.ms-word":
    payload[partition.get_f ilename()] =
    partition.get_p ayload(count,Tr ue)
    count += 1

    message.close()
    except:
    print "Houston we have a problem"
    --- end of snippet

    I've wrote the above segment but haven't tried running it yet because I'm
    unclear about the api for message.
    In the get_payload api it says if you don't pass parameters it returns a
    list of messages (1 for each part)

    it also states a lot of other stuff which I found to be a bit confusing.

    If I use it as above, is it going to return a string? or message object?
    If its a message object, what does it mean to have an decoded message object
    vs an encoded message object?

    finally I want to associate each part's original name with the part (whether
    actual data or a message object) so I was thinking of using a dictionary
    approach



    David
    -------
    Tracfone: http://cellphone.duneram.com/index.html
    Cam: http://www.duneram.com/cam/index.html
    Tax: http://www.duneram.com/index.html

    _______________ _______________ _______________ _______________ _____
    Getting married? Find great tips, tools and the latest trends at MSN Life
    Events. http://lifeevents.msn.com/category.aspx?cid=married


  • fishboy

    #2
    Re: walking a MIME encoded multipart email [python]

    On Thu, 03 Jun 2004 18:31:54 +0000, "David Stockwell"
    <winexpert@hotm ail.com> wrote:
    [color=blue]
    >snippet[/color]
    [snippet][color=blue]
    >I've wrote the above segment but haven't tried running it yet because I'm
    >unclear about the api for message.[/color]

    Ok, let's stop here.

    What?!
    Has Ernst Stavro Blofeld attached some doomsday device to your stack
    trace?
    You've got an interpreter, use it! Go man! Go!

    Anyway, I'll poke around a little for you. I put your post into a
    file named message.txt headers and all.

    $ python
    Python 2.3.3 (#1, Apr 30 2004, 08:54:23)
    [GCC 3.3.1 (cygming special)] on cygwin
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import email
    >>> m = email.message_f rom_file(file(' message.txt'))
    >>> m.keys()[/color][/color][/color]
    ['Path', 'From', 'Newsgroups', 'Subject', 'Date', 'Organization',
    'Lines', 'Message-ID', 'NNTP-Posting-Host', 'Mime-Version',
    'Content-Type', 'X-Trace', 'X-Complaints-To', 'NNTP-Posting-Date',
    'To', 'X-Originating-IP', 'X-Originating-Email', 'X-Sender',
    'X-OriginalArrival Time', 'X-Spam-Status', 'X-BeenThere',
    'X-Mailman-Version', 'Precedence', 'List-Id', 'List-Unsubscribe',
    'List-Archive', 'List-Post', 'List-Help', 'List-Subscribe', 'Xref'][color=blue][color=green][color=darkred]
    >>> m['Date'][/color][/color][/color]
    'Thu, 03 Jun 2004 18:31:54 +0000'[color=blue][color=green][color=darkred]
    >>> m.get_content_t ype()[/color][/color][/color]
    'text/plain'[color=blue][color=green][color=darkred]
    >>> s = m.get_payload()
    >>> print s[/color][/color][/color]
    snippet

    payload = {}
    try:
    message = email.message_f rom_string(mess age)
    messageHeader = dict(message._h eaders)
    [... etc...][color=blue][color=green][color=darkred]
    >>> type(s)[/color][/color][/color]
    <type 'str'>[color=blue][color=green][color=darkred]
    >>> for p in m.walk():[/color][/color][/color]
    .... print p.get_content_t ype()
    ....
    text/plain[color=blue][color=green][color=darkred]
    >>>
    >>> dir(m)[/color][/color][/color]
    ['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__',
    '__len__', '__module__', '__setitem__', '__str__', '_charset',
    '_default_type' , '_get_params_pr eserve', '_headers', '_payload',
    '_unixfrom', 'add_header', 'add_payload', 'as_string', 'attach',
    'del_param', 'epilogue', 'get', 'get_all', 'get_boundary',
    'get_charset', 'get_charsets', 'get_content_ch arset',
    'get_content_ma intype', 'get_content_su btype', 'get_content_ty pe',
    'get_default_ty pe', 'get_filename', 'get_main_type' , 'get_param',
    'get_params', 'get_payload', 'get_subtype', 'get_type',
    'get_unixfrom', 'has_key', 'is_multipart', 'items', 'keys',
    'preamble', 'replace_header ', 'set_boundary', 'set_charset',
    'set_default_ty pe', 'set_param', 'set_payload', 'set_type',
    'set_unixfrom', 'values', 'walk'][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    etc.. etc...

    That should answer a few of your questions. Do not fear the stack
    trace, it is your friend.
    [color=blue]
    ><{{{*>[/color]

    Comment

    • Tim Jarman

      #3
      Doomsday devices considered harmful (Was: walking a MIME encodedmultipar t email [python])

      On Friday 04 Jun 2004 06:15, fishboy wrote:
      [color=blue]
      > What?!
      > Has Ernst Stavro Blofeld attached some doomsday device to your stack
      > trace?
      > You've got an interpreter, use it! Go man! Go!
      >[/color]
      +1 QOTW

      Comment

      Working...