Problems creating mail content using email.MIMEText, non-ASCII encoding

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Wed Jan 14 17:29:00 EST 2004


Hi
I'm trying to create e-mail content using the email.MIMEText module.
It basically works, until I tried to send mail in non-ascii format.

What I did, to test both iso-8859-15 and UTF-8 encodings, was this:

----
from email.MIMEText import MIMEText
m=MIMEText(u"body text including an Euro char \u20ac\n", _charset="iso-8859-15")
m['From'] = "from at mail.invalid"
m['To'] = "to at mail.invalid"
m['Subject'] = "iso 8859-15 encoding?"
print "FIRST MAIL: [[[%s]]]" % m.as_string()

m=MIMEText(u"body text including an Euro char \u20ac\n", _charset="UTF-8")
m['From'] = "from at mail.invalid"
m['To'] = "to at mail.invalid"
m['Subject'] = "UTF-8 encoding?"
print "SECOND MAIL: [[[%s]]]" % m.as_string()
----

But that doesn't work. The output is (using Python 2.3.3):

-----
[E:\temp]python mail.py
FIRST MAIL: [[[Content-Type: text/plain; charset="iso-8859-15"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
From: from at mail.invalid
To: to at mail.invalid
Subject: iso 8859-15 encoding?

body text including an Euro char =20AC
]]]
Traceback (most recent call last):
   File "mail.py", line 12, in ?
     print "SECOND MAIL: [[[%s]]]" % m.as_string()
......snipped.........
   File "C:\Python23\lib\email\base64MIME.py", line 148, in encode
     enc = b2a_base64(s[i:i + max_unencoded])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 
33: ordinal not in range(128)
-----


The first one in iso-8859-15 format looks okay but is in quoted-printable,
and either my mail clients are wrong (unlikely, I've never had this kind
of problems with them) or the content is wrong, because what
I'm seeing this in Mozilla mail and my webmail client:
    "body text including an Euro char  AC"         (a space before 'AC'!)

My UTF-8 attempt failed totally as you can see.

What am I doing wrong?

I'm thinking about ditching email.MIMEText and just create the mail
body text myself using regular unicode --> UTF-8 encoding, but then
I lose the automatic header creation and some more handy things of
email.MimeText...

Thanks for help
--Irmen de Jong.



More information about the Python-list mailing list