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

I

Irmen de Jong

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'] = "(e-mail address removed)"
m['To'] = "(e-mail address removed)"
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'] = "(e-mail address removed)"
m['To'] = "(e-mail address removed)"
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: (e-mail address removed)
To: (e-mail address removed)
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.
 
I

Irmen de Jong

I said:
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.
[...]

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.

I solved it.

First, I concluded that you have to encode the message body yourself
before putting it in the MIMEText object. So I now encode my
unicode message body like this:
mail=MIMEText(body.encode('iso-8859-15','replace'), _charset='iso-8859-15')

Also, I had to override the default 'utf-8' email.Charset to
avoid having the message body encoded in base-64 when using utf-8:

import email.Charset
email.Charset.add_charset( 'utf-8', email.Charset.SHORTEST, None, None )

The resulting emails and headers look very similar to what Mozilla Mail
is generating, and the received emails display well. I'm happy :)

--Irmen
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top