Python's email module - problem with umlauts in some email clients

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nico Grubert

    #1

    Python's email module - problem with umlauts in some email clients

    Hi there,

    I wrote a short python script that sends an email using python's email
    module and I am using Python 2.3.5.
    The problem is, that umlauts are not displayed properly in some email
    clients:

    + On a windows machine running thunderbird 1.0.2 umlauts are displayed
    properly.
    The email header contains "Content-type: text/plain; charset=utf-8"
    so the email client's character encoding automatically switches to
    "Unicode (UTF-8)"

    + On a solaris machine running thunderbird 1.5.0.8 and on a macintosh
    machine running eudora umlauts are *not* displayed properly.
    The email header does not contain any "Content-type". If I manually
    switch the email client's character encoding to "Unicode (UTF-8)", the
    umlauts are displayed properly. Therefore, I guess it has something to
    do with the missing "Content-type: text/plain; charset=utf-8"
    information in the email header.

    Any idea why the "Content-type: text/plain; charset=utf-8" is missing?


    Here is my script:

    #------------------------------------------------------------------
    # send email
    from email.Header import Header
    import email.Message
    import email.Utils
    import mimetypes
    from smtplib import SMTP

    host = 'mail.example.c om'
    mFrom = 'any.sender@exa mple.com'
    mTo = 'foo.bar@exampl e.com'
    mSubj = u'f\xfcr'
    mBody = u'f\xfcr foo bar'
    mBody = mBody.encode('U TF-8')

    mainMsg = email.Message.M essage()

    mainMsg['From'] = mFrom
    mainMsg['To'] = mTo
    mainMsg['Subject'] = mSubj
    mainMsg.set_pay load(mBody)

    mainMsg['Date'] = email.Utils.for matdate(localti me=1)
    mainMsg['Message-ID'] = email.Utils.mak e_msgid()
    mainMsg['Mime-version'] = '1.0'
    mainMsg['Content-type'] = 'text/plain; charset=utf-8'
    mainMsg['Content-transfer-encoding'] = '8bit'
    # 'quoted-printable' does not work either
    # mainMsg['Content-Transfer-Encoding'] = 'quoted-printable'

    s = SMTP(host)
    s.sendmail(mFro m, [mTo], mainMsg.as_stri ng())
    s.close()
    #------------------------------------------------------------------

    Regards,
    Nico
Working...