467,076 Members | 940 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,076 developers. It's quick & easy.

walking a MIME encoded multipart email [python]

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
Jul 18 '05 #1
  • viewed: 2614
Share:
2 Replies
On Thu, 03 Jun 2004 18:31:54 +0000, "David Stockwell"
<wi*******@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.
<{{{*>


Jul 18 '05 #2
On Friday 04 Jun 2004 06:15, fishboy wrote:
What?!
Has Ernst Stavro Blofeld attached some doomsday device to your stack
trace?
You've got an interpreter, use it! Go man! Go!

+1 QOTW

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Gary Smith | last post: by
4 posts views Thread by Alberto | last post: by
1 post views Thread by Zev Steinhardt | last post: by
3 posts views Thread by b. dougherty | last post: by
4 posts views Thread by ceh | last post: by
7 posts views Thread by Ron Garret | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.