email module in 2.2

Matthew Dixon Cowles matt at mondoinfo.com
Sun May 26 01:14:19 EDT 2002


On Sat, 25 May 2002 22:43:41 -0500, Alex Russell <alex at securepipe.com>
wrote:

Dear Alex,

>I've encountered messages where there's a multi-part alternative
>message that's wrapping both text/plain and text/html versions of a
>message. I really don't want the HTML part (security problems
>galore), so I just want to kick back the text/plain sub-sections in
>this case.

That's not uncommon in my experience. And I agree with you about the
security problems. A more difficult question is what to do with mail
that has only an HTML part. The code I posted earlier should ignore
text/html parts and print only text/plain parts. Of course, it doesn't
know whether two parts are from a multipart/mixed message or a
multipart/alternative message since walk() flattens the structure.

>The boundaries (given by Message.get_boundary()) seperate them, but
>short of string.split-ing on that boundary, I'm at a loss for how to
>do this.

The email module gives you various ways to get at the various MIME
parts. It will do the splitting for you but your code has to decide
what parts it cares about and what to do with them.

I posted some code to recursively unpack a MIME message in
ActiveState's Python cookbook. It's at:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/86675

That code only checks to see if a message is multipart but since it
walks the message recursively, it should be relatively straightforward
to have it check to see whether a message is multipart/mixed or
multipart/alternative and do different things depending on which is
the case.

>> At a guess, you may be running into the problem of duplicating the
>> conventions followed by some particular email client or clients.

>I think this may be the case, but being the perfectionist I am, I
>want to do the "right thing" no matter how badly mangled the email
>client may be.

Of course your perfectionism is laudable but it may be thwarted by the
worst-behaved (or worst-configured) mail clients.

If you run into problems figuring out the code or making it do what
you need, please post and I (or someone smarter) will try to help.

Regards,
Matt



More information about the Python-list mailing list