Python:通过gmail发送邮件问题
我正在尝试使用有人曾经在该网站上编写的 python 脚本发送电子邮件(通过 gmail),但出现错误: UnicodeDecodeError:'utf8'编解码器无法解码位置2中的字节0xe8:无效的连续字节
脚本:
import smtplib
from email.mime.text import MIMEText
#mail setup
FROMMAIL = "[email protected]"
LOGIN = FROMMAIL
PASSWORD = "yyy"
SUBJECT = "test subject"
TOMAIL = "[email protected]"
msg = MIMEText('testcontent')
msg['Subject'] = 'test'
msg['From'] = FROMMAIL
msg['To'] = TOMAIL
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMMAIL, [TOMAIL], msg.as_string())
server.quit()
堆栈跟踪:
Traceback (most recent call last):
File "C:\Users\xxx\Desktop\test.py", line 11, in
server = smtplib.SMTP('smtp.gmail.com', 587)
File "C:\Program Files\Python31\lib\smtplib.py", line 248, in __init__
fqdn = socket.getfqdn()
File "C:\Program Files\Python31\lib\socket.py", line 290, in getfqdn
name = gethostname()
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 2: invalid continuation byte
我正在使用python v3.1.3。 如何解决这个问题?
谢谢。
I am trying to send an email (through gmail) using python script that someone once wrote on this site, but I'm getting an error:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 2: invalid continuation byte
the script:
import smtplib
from email.mime.text import MIMEText
#mail setup
FROMMAIL = "[email protected]"
LOGIN = FROMMAIL
PASSWORD = "yyy"
SUBJECT = "test subject"
TOMAIL = "[email protected]"
msg = MIMEText('testcontent')
msg['Subject'] = 'test'
msg['From'] = FROMMAIL
msg['To'] = TOMAIL
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMMAIL, [TOMAIL], msg.as_string())
server.quit()
The stacktrace:
Traceback (most recent call last):
File "C:\Users\xxx\Desktop\test.py", line 11, in
server = smtplib.SMTP('smtp.gmail.com', 587)
File "C:\Program Files\Python31\lib\smtplib.py", line 248, in __init__
fqdn = socket.getfqdn()
File "C:\Program Files\Python31\lib\socket.py", line 290, in getfqdn
name = gethostname()
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 2: invalid continuation byte
I am using python v3.1.3.
How to resolve this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Python 的“电子邮件”模块来生成格式正确的电子邮件。
在直接通过 Python 撰写电子邮件时处理应用程序级别的编码问题并不是正确的方法。
Use the 'email' module of Python in order to generate proper formatted emails.
Dealing yourself with encoding issues on the application level while composing emails directly through Python is not the way to go.