I am trying to send a password reset link to users but I keep getting this error.
My views.py:
def password_reset_request_view(request):
if request.method == 'POST':
email = request.POST.get('email')
if email:
try:
user = CustomUser.objects.get(email=email)
subject = "Password Reset Requested"
email_template_name = "users/password_reset_email.html"
c = {
"email": user.email,
"domain": request.get_host(),
"site_name": "Your Site",
"uid": urlsafe_base64_encode(force_bytes(user.pk)),
"user": user,
"token": default_token_generator.make_token(user),
"protocol": 'http',
}
email_content = render_to_string(email_template_name, c)
logger.debug(f"Sending email to {user.email}")
logger.debug(f"Email content: {email_content}")
# Create an SMTP connection with debug level set to 1
connection = get_connection()
connection.open()
connection.connection.set_debuglevel(1)
send_mail(
subject,
email_content,
settings.DEFAULT_FROM_EMAIL,
[user.email],
fail_silently=False,
connection=connection
)
messages.success(request, 'A message with reset password instructions has been sent to your inbox.')
logger.debug("Email sent successfully")
return redirect('password_reset_done')
except CustomUser.DoesNotExist:
messages.error(request, 'No account found with this email address.')
logger.error("No account found with this email address")
return redirect('reset_password')
return render(request, 'users/password_reset.html')
My settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.mail.yahoo.com' # Your email service SMTP server
EMAIL_PORT = 587 # The port your SMTP server uses (e.g., 587 for TLS)
EMAIL_USE_TLS = True # Use TLS for security
EMAIL_HOST_USER = '[email protected]' # Your email address
EMAIL_HOST_PASSWORD = 'password' # Your email password
DEFAULT_FROM_EMAIL = '[email protected]' # Default from email address
I feel like my code is correct. Why do I keep getting this error?
The code seems correct, the problem is with your mail provider. Change to outlook, gmail etc and see if the error exists.
For outlook, change settings.py to:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "password"
DEFAULT_FROM_EMAIL = "[email protected]"