最近在跟着学习廖雪峰的 Python 教程,到发送邮件这一章出现了编码问题,搜索一直得不到解决,特来求助广大 V 友。
以下是代码部分:
from email.mime.text import MIMEText
msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
# 输入 Email 地址和口令:
from_addr = input('From: ')
password = input('Password: ')
# 输入收件人地址:
to_addr = input('To: ')
# 输入 SMTP 服务器地址:
smtp_server = input('SMTP server: ')
import smtplib
server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
报错:
SMTP server: smtp.qq.com
send: 'ehlo Sakuoz 丶.lan\r\n'
Traceback (most recent call last):
File "F:\github\pythonTest\pytest.py", line 16, in <module>
server.login(from_addr, password)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 693, in login
self.ehlo_or_helo_if_needed()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 599, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 439, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 366, in putcmd
self.send(str)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 351, in send
s = s.encode(self.command_encoding)
UnicodeEncodeError: 'ascii' codec can't encode character '\u4e36' in position 11: ordinal not in range(128)
\u4e36 经过 unicode 转换为中文丶,我发送的邮件( QQ to 163 )昵称中也有个丶,所以怀疑可能与昵称有关,但是经过测试删掉昵称中的丶还是报同样的错误,萌新的我一直不知道错在什么地方。。。请问问题出在哪里,该怎么解决,谢谢大家!
开发环境:
1
gulucn 2017-05-09 18:42:04 +08:00 via Android 1
self.putcmd(self.ehlo_msg, name or self.local_hostname)
你的主机名有中文吧。 |
2
TimePPT 2017-05-09 18:45:02 +08:00
你输入的昵称出现在哪里? from_addr to_addr 都不允许直接传昵称
|
5
sakuoz OP |