views:

41

answers:

2

I have the following code :

msgtxt = "é"

msg = MIMEText(msgtxt)
msg.set_charset('ISO-8859-1')
msg['Subject'] = "subject"
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
serv.sendmail("[email protected]","[email protected]", msg.as_string())

The e-mail arrive with é as its body instead of the expected é

I have tried :

msgtxt = "é".encode("ISO-8859-1")
msgtxt = u"é"
msgtxt = unicode("é", "ISO-8859-1")

all yield the same result.

How to make this work?

Any help is appreciated. Thanks in advance, J.

+1  A: 
msgtxt = "é"
msg.set_charset('ISO-8859-1')

Well, what's the encoding of the source file containing this code? If it's UTF-8, which is a good default choice, just writing the é will have given you the two-byte string '\xc3\xa9', which, when viewed as ISO-8859-1, looks like é.

If you want to use non-ASCII byte string literals in your source file without having to worry about what encoding the text editor is saving it as, use a string literal escape:

msgtxt = '\xE9'
bobince
That was not exactly the problem but i was in fact overlooking the source file encoding, its working now, thank you for the input.J
OldJim
A: 
# coding: utf-8        (or whatever you want to save your source file in)
msgtxt = u"é"
msg = MIMEText(msgtxt,_charset='ISO-8859-1')

Without the u the text will be in the source encoding. As a Unicode string, msgtxt will be encoded in the indicated character set.

Mark Tolonen