Trouble with email package

T

Torsten Bronger

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.
 
T

Torsten Bronger

Hallöchen!

Ingrid said:
[...]

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.
 
H

Harel

Hi there,
What was the solution you found?
Could you please post it? I'm having the same problem... ;o(
Thanks!
Harel

Hallöchen!

Ingrid said:
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.
 
G

Gabriel Genellina

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top