使用'idna'编解码器编码失败(UnicodeError:标签为空或太长)Django send_mail

问题描述 投票:0回答:0

通过send_mail函数向邮件发送消息时,出现错误-encoding with 'idna' codec failed (UnicodeError: label empty or too long) 可能是什么问题以及如何使编码正常工作 最有趣的是,在另一台计算机上一切正常,消息发送没有任何问题,所以错误显然不在 settings.py 中

settings.py 文件:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False

views.py 文件:

def post_share(request, slug):
    post = get_object_or_404(Post, slug=slug, status='published')
    sent = False
    form = EmailPostForm(request.POST or None)
    if request.method == "POST" and form.is_valid():
        cd = form.cleaned_data
        post_url = request.build_absolute_uri(post.get_absolute_url())
        name_user = cd["email"].replace("@gmail.com", "")
        subject = f'Пользователь ({name_user}) рекомендует к прочтению — {post.title}'
        message = f'Прочитайте "{post.title}" по ссылке - {post_url}\n\nСообщение: {cd["comments"]}'
        send_mail(subject, message, settings.EMAIL_HOST_USER, [cd['to']])
        sent = True
    return render(request, 'blog/post/share.html', {'post': post,
                                                    'form': form,
                                                    'sent': sent})

我尝试使用idna库,但是没有用,显示同样的错误

python django unicode backend python-unicode
© www.soinside.com 2019 - 2024. All rights reserved.