使用send_mail发送邮件报编码错误UnicodeEncodeError: 'ascii' codec can't encode characters in

六年级大班 2018-12-06 08:19:37
下面是views.py的代码

#encoding:utf-8
from django.shortcuts import render
from django.core.mail import send_mail
from django.conf import settings
from django.http import HttpResponse
def send_a(request):
# 发邮件
subject = '天天生鲜欢迎信息'
message = ''
sender = settings.EMAIL_FROM #"我的邮箱<1234567@qq,com>"

receiver = ["1234567@qq.com",]
send_mail(subject, message, sender, receiver, fail_silently=False)
return HttpResponse("ok")


下面是我的settings配置的邮箱
# 发送邮件配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# smpt服务地址
EMAIL_HOST = 'smtp.qq.com'
EMAIL_PORT = 25
# 发送邮件的邮箱
EMAIL_HOST_USER = '983****201@qq.com'#授权码和邮箱没有公布出来
# 在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = '***********'#授权码和邮箱没有公布出来
# 收件人看到的发件人
EMAIL_FROM = '天天生鲜<983****201@qq.com>'
#授权码和邮箱没有公布出来

下面是出错信息
Internal Server Error: /
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Program Files\Python35\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\django_project\send_mail_test\myapp\views.py", line 15, in send_a
send_mail(subject, message, sender, receiver, fail_silently=False)
File "C:\Program Files\Python35\lib\site-packages\django\core\mail\__init__.py", line 60, in send_mail
return mail.send()
File "C:\Program Files\Python35\lib\site-packages\django\core\mail\message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Program Files\Python35\lib\site-packages\django\core\mail\backends\smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "C:\Program Files\Python35\lib\site-packages\django\core\mail\backends\smtp.py", line 70, in open
self.connection.login(self.username, self.password)
File "C:\Program Files\Python35\lib\smtplib.py", line 693, in login
self.ehlo_or_helo_if_needed()
File "C:\Program Files\Python35\lib\smtplib.py", line 599, in ehlo_or_helo_if_needed
if not (200 <= self.ehlo()[0] <= 299):
File "C:\Program Files\Python35\lib\smtplib.py", line 439, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "C:\Program Files\Python35\lib\smtplib.py", line 366, in putcmd
self.send(str)
File "C:\Program Files\Python35\lib\smtplib.py", line 351, in send
s = s.encode(self.command_encoding)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-6: ordinal not in range(128)


有尝试过使用import sys

reload(sys)
sys.setdefaultencoding('utf-8')

我的python版本是3.5.2的,不适合我的python版本,希望大神帮忙解决一下
...全文
1372 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wuerselen 2020-05-07
  • 打赏
  • 举报
回复
我的笔记本的电脑不是中文名字呀
skyf**er 2020-05-07
  • 打赏
  • 举报
回复
我的电脑名字也不是, 但也不能发送
wh309458166 2020-03-11
  • 打赏
  • 举报
回复
卧槽,我也在学习天天生鲜这个,也会这个位置,搞了一天都没解决,我电脑名字也是中文....卧槽
六年级大班 2018-12-07
  • 打赏
  • 举报
回复
问题我让我们老师找到了,编程代码没有任何问题,只是我的计算机名是中文的,然后就影响到python了
七月漠南 2018-12-06
  • 打赏
  • 举报
回复
对中文串加Header("xxx","utf-8")

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧