Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problems creating mail content using email.MIMEText, non-ASCII encoding

Reply
Thread Tools

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

 
 
Irmen de Jong
Guest
Posts: n/a
 
      01-14-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'] = "(E-Mail Removed)"
m['To'] = "(E-Mail 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 Removed)"
m['To'] = "(E-Mail 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: http://www.velocityreviews.com/forums/(E-Mail Removed)lid
To: (E-Mail Removed)lid
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(12
-----


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.
 
Reply With Quote
 
 
 
 
Irmen de Jong
Guest
Posts: n/a
 
      01-18-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
changing JVM encoding; setting -Dfile.encoding doesn't work pasmol@plusnet.pl Java 1 10-08-2004 09:50 PM
mail headers to automatically detect the encoding/charset for mail clients sunil Java 0 07-28-2004 08:43 PM
Encoding.Default and Encoding.UTF8 Hardy Wang ASP .Net 5 06-09-2004 04:04 PM
System.web.mail mail content disappear me ASP .Net 0 06-04-2004 11:38 AM



Advertisments