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

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Sun Jan 18 10:18:58 EST 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



More information about the Python-list mailing list