我需要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')
答案 0 :(得分:1)
如果这是您的实际代码,您可以将电子邮件发送到&#34;电子邮件&#34; ,而不是之前定义的email
变量的值。
所以,
msg = EmailMessage('Request Callback','Here is the message.', to=[email, ])