0

I'm trying to add change password functionality to a Django / React app. I'm using django-rest-passwordreset library for creating Reset or Forgot Password API using Django Rest Framework.

After I type the email that registered, I've managed get the token from the console enter image description here

But I'm struggling to find a way to send an email to their email address with the link to reset their password along the given token. I've tried the SMTP Method or SendGrid. Was trying this way and this way

Can someone show me the correct way to make this work? Thanks a lot.

Edit :

Settings.py

INSTALLED_APPS = [
    ...
    'rest_framework',
    'django_rest_passwordreset',
]

Urls.py

from django.urls import path, include
urlpatterns = [
    ...
    path('api/password_reset/', include('django_rest_passwordreset.urls', namespace='password_reset')),
]

Models.py

from django.dispatch import receiver
from django.urls import reverse
from django_rest_passwordreset.signals import reset_password_token_created
from django.core.mail import send_mail  


@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):

    email_plaintext_message = "{}?token={}".format(reverse('password_reset:reset-password-request'), reset_password_token.key)

    send_mail(
        # title:
        "Password Reset for {title}".format(title="Astect"),
        # message:
        email_plaintext_message,
        # from:
        "[email protected]",
        # to:
        [reset_password_token.user.email]
    )
2
  • Please provide the code you have wrote so far to accomplish your task and specify your issue specifically. Commented Jan 20, 2021 at 9:02
  • Hey, I've provided the code. Thanks. Commented Jan 20, 2021 at 9:09

1 Answer 1

0

You can make like this

@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
    
    # send an e-mail to the user
    context = {
        "current_user": reset_password_token.user,
        "username": reset_password_token.user.username,
        "email": reset_password_token.user.email,
        "reset_password_url": "{}?token={}".format(
            instance.request.build_absolute_uri(reverse("password_reset:reset-password-confirm")),
            reset_password_token.key,
        ),
    }

    # render email text
    email_html_message = render_to_string("email/user_reset_password.html", context)
    email_plaintext_message = render_to_string("email/user_reset_password.txt", context)

    msg = EmailMultiAlternatives(
        # title:
        "Password Reset for {title}".format(title="Pinper"),
        # message:
        email_plaintext_message,
        # from:
        "<youremail>",
        # to:
        [reset_password_token.user.email],
    )
    msg.attach_alternative(email_html_message, "text/html")
    msg.send()

And create a template like this

....
....
<body>
    <h1>Reset password for {{current_user}}</h1>
    <h4>You write your message</h4>

    {{ reset_password_url }}

</body>

The template receive a context with some variables: current_user, username, email, reset_password_url .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.