0

I have the following code :

msgtxt = "é"

msg = MIMEText(msgtxt)
msg.set_charset('ISO-8859-1')
msg['Subject'] = "subject"
msg['From'] = "from@mail.com"
msg['To'] = "to@mail.com"
serv.sendmail("from@mail.com","to@mail.com", 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.

OldJim
  • 331
  • 1
  • 2
  • 12

2 Answers2

1
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
  • 528,062
  • 107
  • 651
  • 834
  • 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 Sep 15 '10 at 23:48
0
# 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
  • 166,664
  • 26
  • 169
  • 251