Reputation: 189
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.
Upvotes: 1
Views: 1210
Reputation: 20792
Yes, as you write in https://stackoverflow.com/a/18573582/1346705. Python 3.3 contains in MIMEText.__init__()
# If no _charset was specified, check to see if there are non-ascii
# characters present. If not, use 'us-ascii', otherwise use utf-8.
# XXX: This can be removed once #7304 is fixed.
if _charset is None:
try:
_text.encode('us-ascii')
_charset = 'us-ascii'
except UnicodeEncodeError:
_charset = 'utf-8'
Python 3.2 does not contain that specific code.
The reason why the charset should be specified (explicitly or fixed like in 3.3 or somehow) is that sometimes open
and the like functions use the OS prefered encoding. It may complicate the things. Also, the print
in your case may cause similar problems as a console usually does not use Unicode.
Upvotes: 0
Reputation: 189
The solution is to change line 7 from
msg = MIMEText(emailTextHTML, 'html')
to
msg = MIMEText(emailTextHTML, 'html', 'utf-8')
Now it works in both environments.
Python bugs 7304 and 14380 seems to be related to this. So my problem was more of a Python 3.2 to 3.3 issue.
Upvotes: 2