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:
        "astect@asei.co.id",
        # to:
        [reset_password_token.user.email]
    )
Syafiqur__
  • 531
  • 7
  • 15

1 Answers1

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 .