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

Discussion in 'Python' started by Irmen de Jong, Jan 14, 2004.

  1. 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'] = ""
    m['To'] = ""
    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'] = ""
    m['To'] = ""
    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: lid
    To: 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(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.
    Irmen de Jong, Jan 14, 2004
    #1
    1. Advertising

  2. Re: Problems creating mail content using email.MIMEText, non-ASCIIencoding

    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
    Irmen de Jong, Jan 18, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damjan

    Unicode in MIMEText

    Damjan, Nov 24, 2005, in forum: Python
    Replies:
    5
    Views:
    531
    Damjan
    Nov 28, 2005
  2. TOXiC
    Replies:
    5
    Views:
    1,244
    TOXiC
    Jan 31, 2007
  3. Marcus.CM

    py2exe bug with email.MIMEText

    Marcus.CM, Aug 1, 2008, in forum: Python
    Replies:
    0
    Views:
    241
    Marcus.CM
    Aug 1, 2008
  4. bruce
    Replies:
    38
    Views:
    270
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    95
Loading...

Share This Page