Unicode - and MIMEType - Good friday fun.

R

rh0dium

Hi Geniuses,

Can anyone please show me the way.. I don't understand why this
doesn't work...


# encoding: utf-8
from email.MIMEText import MIMEText

msg = MIMEText("hi")
msg.set_charset('utf-8')
print msg.as_string()

a = 'Ho\xcc\x82tel Ste\xcc\x81phane '
b = unicode(a, "utf-8")

print b

msg = MIMEText(b)
msg.set_charset('utf-8')
print msg.as_string()

It should right??

Thanks!
 
M

MRAB

rh0dium said:
Hi Geniuses,

Can anyone please show me the way.. I don't understand why this
doesn't work...


# encoding: utf-8
from email.MIMEText import MIMEText

msg = MIMEText("hi")
msg.set_charset('utf-8')
print msg.as_string()

a = 'Ho\xcc\x82tel Ste\xcc\x81phane '
b = unicode(a, "utf-8")

print b

msg = MIMEText(b)
msg.set_charset('utf-8')
print msg.as_string()

It should right??
'b' is Unicode, but you're telling 'msg' that it's UTF-8, which it
isn't. Try giving 'msg' the UTF-8 string:
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="utf-8"

HoÌ‚tel SteÌphane
 
R

Rami Chowdhury

b = unicode(a, "utf-8")

[snip]
msg = MIMEText(b)

I believe this is the problem line -- the MIMEText constructor takes
encoded strings rather than unicode objects. Try:

msg = MIMEText(a)

Or, alternatively

msg = MIMEText(b.encode('utf-8'))
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top