POP text/plain

Tim Williams listserver at tdw.net
Wed Oct 20 12:53:44 EDT 2004


----- Original Message ----- 
From: "Tim Williams" <listserver at tdw.net>
>
> "LutherRevisited" <lutherrevisited at aol.com> wrote in message
> news:20041020010134.07554.00002191 at mb-m18.aol.com...
> > Ok, what I do for most email messages is get the content that is type
> > 'text/html', which is 99% of my messages, and with string functions I
can
> get a
> > string from that which is only html code.  But, for plain text messages,
> the
> > part which is text/plain doesn't really leave me with anything I can
think
> of
> > to get a substring from that which is only the body of the message.  I
> always
> > end up with header and all when I display a plaintext message.  Any
> thoughts?
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
> Use the email module to split any email into its "parts",  then work with
> the parts individually

[snip bad code]

After further thoughts on this, and time to wake-up properly

(its still untested,  but better than the last example  and should give you
something to build on)

import email as e
import mimetypes
from email.MIMEText import MIMEText

msg = e.message_from_string(whole_msg)
new_payload = []

if not msg.get_content_type() == 'text/plain':
    for part in msg.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get_content_type() == 'text/plain':
            new_payload.append(part)  # keep this part

    if not new_payload:
        new_payload = [MIMEText('\t\tText-only filter was applied\r\n')]

    msg.epilogue = ''
    msg.set_payload(new_payload)

print msg




More information about the Python-list mailing list