I'm creating a user account programmatically using the code below (Drupal 8). Everything works well except when the user suppose to change their password via the Drupal generated 'one-time login link'. The password reset doesn't work and it seems the 'pass-reset-token' is not generated properly. What am I doing wrong?

public function submitForm(array &$form, FormStateInterface $form_state)
        $the_user_email = $form_state->getValue('user_email');
        $the_user_name = $form_state->getValue('user_name');
        $language = \Drupal::languageManager()->getCurrentLanguage()->getId();        

        $user = \Drupal\user\Entity\User::create();
        $user->set("init", $the_user_email);
        $user->set("langcode", $language);
        $user->set("preferred_langcode", $language);
        $user->set("preferred_admin_langcode", $language);

        //Create the account

        _user_mail_notify('register_no_approval_required', $user);

        drupal_set_message($this->t('A welcome message with further instructions has been sent to your email address.'));

1 Answer 1


I guess you could utilize function user_pass_reset_url of user.module to generate reset link and send it via your custom _user_mail_notify function.

Generates a unique URL for a user to log in and reset their password.


\Drupal\user\UserInterface $account: An object containing the user account.

array $options: (optional) A keyed array of settings. Supported options are:

langcode: A language code to be used when generating locale-sensitive URLs. If langcode is NULL the users preferred language is used.

Return value

string A unique URL that provides a one-time log in for the user, from which they can change their password.

  • Thank you @sarathkm I'm now sorted. Indeed I wasn't generating the reset url properly which should be done using user_pass_reset_url();
    – Tshwarelo
    Aug 1, 2017 at 9:34
  • For me I had 'Access denied' when used the raw renerated user reset link. Fixed that with adding a missed url part to the generated one, so final url should looks like user/reset/8/1555946938/gpBO10hf23sA0VkOakPCvEV9qSrWRhP-WubMYgeOQ28/login Apr 24, 2019 at 9:26

Your Answer

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

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