You are here

function recovery_pass_forgot_password_submit in Recovery Password (Email New Password) 7

Same name and namespace in other branches
  1. 8 recovery_pass.module \recovery_pass_forgot_password_submit()

Custom submit handler to send password in recovery mail.

1 string reference to 'recovery_pass_forgot_password_submit'
recovery_pass_form_user_pass_alter in ./recovery_pass.module
Implements hook_form_FORM_ID_alter().

File

./recovery_pass.module, line 61
Alters default Drupal password recovery process by overriding default submit.

Code

function recovery_pass_forgot_password_submit($form, &$form_state) {
  global $language;
  $user = $form_state['values']['account'];

  // Generate random password.
  $random_password = user_password();

  // Store Old Hash Password Temporarily.
  if (!_recovery_pass_store_old_pass($user)) {
    watchdog('recovery_pass', 'Error saving old password for user @id', array(
      '@id' => $user->uid,
    ), WATCHDOG_NOTICE, 'link');
  }

  // Save new password.
  user_save($user, array(
    'pass' => $random_password,
  ), 'account');

  // Retrive email body and subject.
  $message = _recovery_pass_mail_text('email_text', $language, TRUE, $user);
  if ($message) {

    // Replace [user_new_password] placeholder with new password.
    $message = str_replace("[user_new_password]", $random_password, $message);
  }
  $subject = _recovery_pass_mail_text('email_subject', $language, TRUE, $user);
  if (module_exists("htmlmail")) {

    // For html mail convert new lines to br.
    $message = nl2br($message);
  }
  $params = array(
    'body' => $message,
    'subject' => $subject,
  );
  $to = $user->mail;
  $from = variable_get('site_mail');
  if (drupal_mail('recovery_pass', 'recovery_pass', $to, language_default(), $params, $from, TRUE)) {
    drupal_set_message(t("Further instructions have been sent to your registered Email-id."), 'status', FALSE);
  }
  else {
    drupal_set_message(t("Error Sending Recovery Mail. Please contact site administrator."), 'error', FALSE);
  }
  $form_state['redirect'] = variable_get('recovery_pass_fpass_redirect', 'user');
}