Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trouble with email package

0 views
Skip to first unread message

Torsten Bronger

unread,
Jul 16, 2007, 9:43:38 AM7/16/07
to
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: bro...@jabber.org
(See http://ime.webhop.org for ICQ, MSN, etc.)

Torsten Bronger

unread,
Jul 16, 2007, 9:53:02 AM7/16/07
to
Hallöchen!

Ingrid Bronger writes:

> [...]


>
> 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 found the cause of this part of my trouble.

Harel

unread,
Jul 24, 2007, 4:10:31 AM7/24/07
to
Hi there,
What was the solution you found?
Could you please post it? I'm having the same problem... ;o(
Thanks!
Harel

On Jul 16, 2:53 pm, Torsten Bronger <bron...@physik.rwth-aachen.de>
wrote:


> Hallöchen!
>
> Ingrid Bronger writes:
> > [...]
>
> > 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 found the cause of this part of my trouble.
>
> Tschö,
> Torsten.
>
> --
> Torsten Bronger, aquisgrana, europa vetus

> Jabber ID: bron...@jabber.org
> (Seehttp://ime.webhop.orgfor ICQ, MSN, etc.)


Gabriel Genellina

unread,
Jul 24, 2007, 8:09:06 PM7/24/07
to pytho...@python.org
En Tue, 24 Jul 2007 05:10:31 -0300, Harel <harel...@gmail.com> escribió:

> What was the solution you found?
> Could you please post it? I'm having the same problem... ;o(
>

> On Jul 16, 2:53 pm, Torsten Bronger <bron...@physik.rwth-aachen.de>
> wrote:
>>
>> > 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

In case he's not reading anymore, this is an extract from the Message
class documentation <http://docs.python.org/lib/module-email.message.html>:

__setitem__(name, val): Add a header to the message [...] Note that this
does not overwrite or delete any existing header with the same name. If
you want to ensure that the new header is the only one present in the
message with field name name, delete the field first, e.g.:

del msg['subject']
msg['subject'] = 'Python roolz!'

See also the add_header and replace_header methods.

--
Gabriel Genellina

0 new messages