Trouble with email package

Torsten Bronger bronger at physik.rwth-aachen.de
Mon Jul 16 09:43:38 EDT 2007


Hallöchen!

I thought that with the following code snippet, I could generate a
MIME email stub:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from email.MIMEText import MIMEText
    from email.Generator import Generator
    import sys

    message = MIMEText(u"Hallöchen!", _charset="utf-8")
    Generator(sys.stdout).flatten(message)

However, MIMEText doesn't see that u"Hallöchen!" is a unicode and
encodes it accoring to _charset.  Additionally, it is encoded as
base64 (why?).  But okay, I added some lines to work around it:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from email.MIMEText import MIMEText
    from email.Generator import Generator
    from email.Charset import Charset
    import sys

    charset = Charset("utf-8")
    charset.body_encoding = None
    message = MIMEText(u"Hallöchen!".encode("utf-8"), _charset="utf-8")
    message.set_charset(charset)
    Generator(sys.stdout).flatten(message)

However, the result of this is

    Content-Type: text/plain; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: base64

    Hallöchen!

The Content-Transfer-Encoding is wrong.  Okay (well, not okay but)
then I added message["Content-Transfer-Encoding"] = "8bit" to my
code and got

    Content-Type: text/plain; charset="utf-8"
    MIME-Version: 1.0
    Content-Transfer-Encoding: base64
    Content-Transfer-Encoding: 8bit

    Hallöchen!

I mean, I can work around anything, but is the email package just
rather buggy or do I something completely misguided here?  Thanks
for any hints!

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
                                      Jabber ID: bronger at jabber.org
                      (See http://ime.webhop.org for ICQ, MSN, etc.)



More information about the Python-list mailing list