This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2008-11-12.13:16:29
SpamBayes Score 5.459967e-06
Marked as misclassified No
Message-id <1226495792.65.0.232994796976.issue4306@psf.upfronthosting.co.za>
In-reply-to
Content
I never used the email package, so my issue is maybe not a bug. I'm 
trying to send an email with diacritics in the subject and the body. 
I'm french so it's natural to use characters not in the ASCII range. I 
wrote this small program:

def main():
    # coding: utf8
    ADDRESS = 'victor.stinner@haypocalc.com'
    from email.mime.text import MIMEText
    msg = MIMEText('accent éôŁ', 'plain', 'utf-8')
    msg['Subject'] = 'sujet éôł'
    msg['From'] = ADDRESS
    msg['To'] = ADDRESS
    text = msg.as_string()
    print("--- FLATTEN ---")
    print(text)
    return
    import smtplib
    client=smtplib.SMTP('smtp.free.fr')
    client.sendmail(ADDRESS, ADDRESS, text)
    client.quit()
main()

(remove the "return" to really send the email)

The problem:
  (...)
  File "/home/haypo/prog/py3k/Lib/email/generator.py", line 141, in 
_write_headers
    header_name=h, continuation_ws='\t')
  File "/home/haypo/prog/py3k/Lib/email/header.py", line 189, in 
__init__
    self.append(s, charset, errors)
  File "/home/haypo/prog/py3k/Lib/email/header.py", line 262, in 
append
    input_bytes = s.encode(input_charset, errors)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 
6-8: ordinal not in range(128)

I don't understand why it uses ASCII whereas I specified that I would 
like to use the UTF-8 charset.

My attached patch reused the message charset to encode the headers, 
but use ASCII if the header can be encoded as ASCII. The patch 
included an unit test.
History
Date User Action Args
2008-11-12 13:16:32vstinnersetrecipients: + vstinner
2008-11-12 13:16:32vstinnersetmessageid: <1226495792.65.0.232994796976.issue4306@psf.upfronthosting.co.za>
2008-11-12 13:16:31vstinnerlinkissue4306 messages
2008-11-12 13:16:31vstinnercreate