8

I implemented Decoupled Drupal in one of my projects, one of the features I need to implement is a forget password feature. So I created a custom route to listen to reset password requested.

I know I can create a reset password URL with user_pass_reset_url(); I can generate this link and programmatically set the email to that user, but I am looking for a service or Drupal core functionality to achieve this job.

How can I programmatically send a reset password email?

  • The way to send a reset password email programmatically is to create a module to change the host and path of the URL so that it links to your app instead, and then provide an endpoint to handle ingesting the user pass reset URL parts. There is no core functionality for this. In Drupal 7, I found it easier to redirect the user from the back end site to the front end app after visiting the /user page – mradcliffe Sep 22 '16 at 11:43
10

With some reverse engineering on core user module the solution is :

    $name = \Drupal::request()->get("name");
    $langcode =  \Drupal::languageManager()->getCurrentLanguage()->getId();
    // Try to load by email.
    $users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(array('mail' => $name));
    if (empty($users)) {
        // No success, try to load by name.
        $users = \Drupal::entityTypeManager()->getStorage('user')->loadByProperties(array('name' => $name));
    }
    $account = reset($users);
    // Mail one time login URL and instructions using current language.
    $mail = _user_mail_notify('password_reset', $account, $langcode);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.