Django 不向有效地址发送电子邮件

Django not sending emails to valid address

我需要 Django 发送确认电子邮件,但它一直告诉我我的电子邮件地址无效 - 我的代码有问题吗?

错误:

SMTPRecipientsRefused at /accounts/signup

{'=?utf-8?q?email?=': (553, b'5.1.2 The recipient address <=?utf-8?q?email?=> is not a valid RFC-5321\n5.1.2 address. h7-v6sm4740542wmc.44 - gsmtp')}

Views.py函数:

def signup(request):
    if request.method == 'POST':
        if request.POST['password1'] == request.POST['password2']:
            try:
                user = User.objects.get(username=request.POST['username'])
                return render(request, 'accounts/signup.html', {'error':'Username has already been taken'})
            except User.DoesNotExist:
                user = User.objects.create_user(request.POST['username'], password=request.POST['password1'], email=request.POST['email'])
                email = user.email
                auth.login(request, user)
                msg = EmailMessage('Request Callback',
                       'Here is the message.', to=['email'])
                msg.send()
                return redirect('home')
        else:  #<---- I think you need this one too
            return render(request, 'accounts/signup.html', {'error':"Passwords didn't match"})
    else:
        #if it's a GET
        return render(request, 'accounts/signup.html')

如果这是您的实际代码,您将发送电子邮件至 "email",而不是您之前定义的 email 变量的值。

所以,

msg = EmailMessage('Request Callback','Here is the message.', to=[email, ])