By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

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

P: n/a
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'] = "fr**@mail.invalid"
m['To'] = "to@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'] = "fr**@mail.invalid"
m['To'] = "to@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: fr**@mail.invalid
To: to@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.
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I wrote:
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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.