Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!news3.google.com!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail From: Helmut Jarausch Newsgroups: comp.lang.python Subject: email with a non-ascii charset in Python3 ? Date: 15 Aug 2012 12:16:44 GMT Lines: 53 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.dfncis.de pF1ho8RfknQ/KCZzD+h+Nw4MPMPvLQFNGWX1zk0zYMRrGLLzybTzUTgMmPFHG/KAs+MYxW9Exl Cancel-Lock: sha1:C+hhKASgBbKYYv/vd7bXX3PFF1g= User-Agent: Pan/0.137 (The Mattel and Mars Bar Quick Energy Chocobot Hour; GIT 9a6aef1 git://git.gnome.org/pan2) X-Received-Bytes: 2626 Xref: csiph.com comp.lang.python:27091 Hi, I'm sorry to ask such a FAQ but still I couldn't find an answer - neither in the docs nor the web. What's wrong with the following script? Many thanks for a hint, Helmut. #!/usr/bin/python3 #_*_ coding: latin1 _*_ import smtplib from email.message import Message import datetime msg= Message() msg.set_charset('latin-1') msg['Subject'] = "*** Email Test ***" msg['From'] = "Email_Tester@numa-sv.igpm.rwth-aachen.de" msg['To'] = "jarausch@igpm.rwth-aachen.de" msg['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S %p') server= smtplib.SMTP("igpm.igpm.rwth-aachen.de") msg.set_payload("Gedanken über einen Test","iso-8859-1") ## I have tried msg.set_payload("Gedanken über einen Test".encode("iso-8859-1"),"iso-8859-1") ## which fails, as well server.send_message(msg) Traceback (most recent call last): File "./Test_EMail_Py3.py", line 17, in server.send_message(msg) File "/usr/lib64/python3.2/smtplib.py", line 812, in send_message g.flatten(msg_copy, linesep='\r\n') File "/usr/lib64/python3.2/email/generator.py", line 91, in flatten self._write(msg) File "/usr/lib64/python3.2/email/generator.py", line 137, in _write self._dispatch(msg) File "/usr/lib64/python3.2/email/generator.py", line 163, in _dispatch meth(msg) File "/usr/lib64/python3.2/email/generator.py", line 396, in _handle_text super(BytesGenerator,self)._handle_text(msg) File "/usr/lib64/python3.2/email/generator.py", line 201, in _handle_text self.write(payload) File "/usr/lib64/python3.2/email/generator.py", line 357, in write self._fp.write(s.encode('ascii', 'surrogateescape')) UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 9: ordinal not in range(128) server.quit() This is Python 3.2.4 (GIT 20120805)