I have this little Python 3 code:
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
emailTextHTML = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"><title>Wöchentliche Ticketbenachrichtigung</title></head><body><p>Hallo ...,</p></body></html>'
msg = MIMEText(emailTextHTML, 'html')
msg['Subject'] = 'TEST Wöchentliche Ticketbenachrichtigung TEST'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
s = smtplib.SMTP('192.168.115.99')
#try:
s.send_message(msg)
#except:
print(msg)
s.quit()
Now the problem is that it runs fine with Python 3.3.2 on Windows 7 x64, but it fails with Python 3.2.3 on Debian Linux x64. I get this error when using the last setup:
Traceback (most recent call last):
File "testing.py", line 13, in <module>
s.send_message(msg)
File "/usr/lib/python3.2/smtplib.py", line 812, in send_message
g.flatten(msg_copy, linesep='\r\n')
File "/usr/lib/python3.2/email/generator.py", line 91, in flatten
self._write(msg)
File "/usr/lib/python3.2/email/generator.py", line 137, in _write
self._dispatch(msg)
File "/usr/lib/python3.2/email/generator.py", line 163, in _dispatch
meth(msg)
File "/usr/lib/python3.2/email/generator.py", line 398, in _handle_text
super(BytesGenerator,self)._handle_text(msg)
File "/usr/lib/python3.2/email/generator.py", line 201, in _handle_text
self.write(payload)
File "/usr/lib/python3.2/email/generator.py", line 357, in write
self._fp.write(s.encode('ascii', 'surrogateescape'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 188: ordinal not in range(128)
The german umlaut in the string is causing this. But why does it succeed on Windows and fail on Linux? What can I do to make the code compatible with both environments? Console encoding doesn't seem to be of relevance here, I suppose.