walking a MIME encoded multipart email [python]

  • Thread starter David Stockwell
  • Start date
D

David Stockwell

snippet

payload = {}
try:
message = email.message_from_string(message)
messageHeader = dict(message._headers)

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

if messageId is not None:
if addressFrom is not None:
if addressReply is not None:
if message.is_multipart:
count = 0
for partition in message.walk():
if partition.get_type() == "application/vnd.ms-word":
payload[partition.get_filename()] =
partition.get_payload(count,True)
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
 
F

fishboy

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.['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...].... print p.get_content_type()
....
text/plain['__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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top