'utf-8' codec can't decode byte 0xe9 in position 4: unexpected end of data

吃可爱长大的小学妹 提交于 2021-01-28 10:32:17

问题


Im very new to this language and encountered some trouble to send emails through python, my code looks like this:

import smtplib
server=smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login('username','password')

message = "hi there"
server.sendmail('username', 'username', message)

server.quit()

my system is: Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32

and the output I get is:

Traceback (most recent call last):
File "C:\pythonfiles\testmail - kopia.py", line 6, in <module>
  server=smtplib.SMTP("smtp.gmail.com",587)
File "C:\python\lib\smtplib.py", line 261, in __init__
  fqdn = socket.getfqdn()
File "C:\python\lib\socket.py", line 676, in getfqdn
  hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in 
position 4: unexpected end of data

My computername consists of the letter é can i have to do something with that? Ive saved the file through notepad to UTF-8.

Any ideas?

Cheers André


回答1:


utf-8 is probably not the right encoding. This is a current problem when parsing files etc including french writting (as I suppose is the case you mention)

I usually solve this problem by :

mystring = 'gdjéàếè'
new_string = mystring.encode('iso-8859-1')

Otherwise, you may want to check this out: How to set a charset in email using smtplib in Python 2.7?



来源:https://stackoverflow.com/questions/51843103/utf-8-codec-cant-decode-byte-0xe9-in-position-4-unexpected-end-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!