Mark Sapiro wrote:
>
>What you want is more like the attached flatten.py.txt file (.txt added
>for content filtering).


Sorry, I forgot the attachment. Here it is.


>Note that this is far from production quality
>and probably doesn't even work on some messages.
>
>Problems I am aware of are things like
>
>- no i18n for canned text strings
>
>- signatures will get broken
>
>- with multipart/alternative, the text/plain part will be aggregated
>with the other text/plain parts and the text/html or other
>alternatives will be separately attached.
>
>- text/plain parts without a specified charset will not be aggregated
>but will be separately attached. This is a difficult issue because
>many mainstream MUAs will attach an arbitrary .txt attachment without
>specifying a charset. If you then assume it is say iso-8859-1 and
>convert it to unicode and in fact it was euc-jp or koi8-r or even
>utf-8, you can garble it irreversably.
>
>flatten.py is written so that it could be installed as is in Mailman as
>a custom Handler. See
><http://www.python.org/cgi-bin/faqw-mm.py?req=show&file=faq04.067.htp>.
>
>Note that this will not address separate attachment of headers and
>footers. If the resultant 'flattened' message is multipart for any
>reason, msg_header and msg_footer will still be attached as separate
>MIME parts.
>
>The basic flow in the process is
>
>If this is not a multipart message do nothing.
>
>Walk through the message making two lists of elemental parts
>  plain_parts are those text/plain parts with known character set
>  other_parts are the rest.
>
>If there were no plain_parts, make a Unicode text part that says so,
>otherwise convert all the plain_parts to Unicode and string them
>together with a separator to make a text part.
>
>If there were no other_parts make the message a single part text/plain
>message with the text payload utf-8 encoded, else make a
>multipart/mixed message with a text/plain part with the text payload
>utf-8 encoded followed by all the other_parts.

-- 
Mark Sapiro <[EMAIL PROTECTED]>        The highway is for gamblers,
San Francisco Bay Area, California    better use your sense - B. Dylan

from email.MIMEText import MIMEText

def process(mlist, msg, msgdata):
    if not msg.is_multipart():
        return
    plain_parts = []
    other_parts = []
    for part in msg.walk():
        if part.is_multipart():
            continue
        if part.get_content_type() == 'text/plain' and 
part.get_content_charset():
            plain_parts.append(part)
        else:
            other_parts.append(part)
    if not plain_parts:
        text_payload = u'Original message had no plain text parts'
    else:
        text_payload = u''
        for part in plain_parts:
            pl = part.get_payload(decode=True)
            cset = part.get_content_charset()
            pl = unicode(pl, cset, 'replace')
            if text_payload:
                text_payload += u'\n-------------next part-----------\n'
            text_payload += pl
        text_payload = text_payload.encode('utf-8')
    if other_parts:
        other_parts.insert(0, MIMEText(text_payload, 'plain', 'utf-8'))
        del msg['content-type']
        del msg['content-transfer-encoding']
        msg['Content-Type'] = 'multipart/mixed'
        msg.set_payload(other_parts)
    else:
        del msg['content-type']
        del msg['content-transfer-encoding']
        msg['Content-Type'] = 'text/plain'
        msg.set_payload(text_payload)
        msg.set_charset('utf-8')
    return
------------------------------------------------------
Mailman-Users mailing list
Mailman-Users@python.org
http://mail.python.org/mailman/listinfo/mailman-users
Mailman FAQ: http://www.python.org/cgi-bin/faqw-mm.py
Searchable Archives: http://www.mail-archive.com/mailman-users%40python.org/
Unsubscribe: 
http://mail.python.org/mailman/options/mailman-users/archive%40jab.org

Security Policy: 
http://www.python.org/cgi-bin/faqw-mm.py?req=show&amp;file=faq01.027.htp

Reply via email to