walking a MIME encoded multipart email [python]

fishboy fishboy at spamspamspam.com
Fri Jun 4 01:15:07 EDT 2004


On Thu, 03 Jun 2004 18:31:54 +0000, "David Stockwell"
<winexpert at hotmail.com> wrote:

>snippet
[snippet]
>I've wrote the above segment but haven't tried running it yet because I'm 
>unclear about the api for message.

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.
>>> import email
>>> m = email.message_from_file(file('message.txt'))
>>> m.keys()
['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-OriginalArrivalTime', 'X-Spam-Status', 'X-BeenThere',
'X-Mailman-Version', 'Precedence', 'List-Id', 'List-Unsubscribe',
'List-Archive', 'List-Post', 'List-Help', 'List-Subscribe', 'Xref']
>>> m['Date']
'Thu, 03 Jun 2004 18:31:54 +0000'
>>> m.get_content_type()
'text/plain'
>>> s = m.get_payload()
>>> print s
snippet

       payload = {}
       try:
         message = email.message_from_string(message)
         messageHeader = dict(message._headers)
[... etc...]
>>> type(s)
<type 'str'>
>>> for p in m.walk():
...     print p.get_content_type()
... 
text/plain
>>>
>>> dir(m)
['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__',
'__len__', '__module__', '__setitem__', '__str__', '_charset',
'_default_type', '_get_params_preserve', '_headers', '_payload',
'_unixfrom', 'add_header', 'add_payload', 'as_string', 'attach',
'del_param', 'epilogue', 'get', 'get_all', 'get_boundary',
'get_charset', 'get_charsets', 'get_content_charset',
'get_content_maintype', 'get_content_subtype', 'get_content_type',
'get_default_type', '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_type', 'set_param', 'set_payload', 'set_type',
'set_unixfrom', 'values', 'walk']
>>>

etc.. etc...

That should answer a few of your questions.  Do not fear the stack
trace, it is your friend.

><{{{*>




More information about the Python-list mailing list