0

I have a register form that I am able to submit programmatically without any problems.

However when I try to do the same with the user reset password form, it doesn't send me any emails, nor does it do any validation.

Here's the code I'm using:

$form_state = array();
$form_state['values']['name'] = $_POST['username'];
$form_state['values']['submit'] = t('E-mail me new password');

drupal_form_submit('user_pass_form', $form_state);

Note: I've tried using user_pass instead of user_pass_form.

What am I doing wrong?

1

An alternative approach is this:

global $language;
$account = user_load_by_name($username);
if ($account) {
// Mail one time login URL and instructions using current language.
$mail = _user_mail_notify('password_reset', $account, $language);
  if (!empty($mail)) {
    watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
  }
}

Essentially all you need is:

_user_mail_notify('password_reset', $account, $language);

More info: user_pass_submit

1

Have you verified that the username you are submitting is valid? If it isn't, this will fail validation. In addition, it is a bad practice to use $_POST directly in Drupal. It should also definitely be drupal_form_submit('user_pass',... not 'user_pass_form'.

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.