function recovery_pass_forgot_password_submit in Recovery Password (Email New Password) 8
Same name and namespace in other branches
- 7 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 36 - Contains module code.
Code
function recovery_pass_forgot_password_submit(array &$form, FormState $form_state) {
$token_service = \Drupal::token();
$language_manager = \Drupal::languageManager();
$user = $form_state
->getValue('account');
$token_options = [
'langcode' => $language_manager
->getCurrentLanguage($user
->getPreferredLangcode())
->getId(),
'callback' => '',
'clear' => TRUE,
];
$config = \Drupal::config('recovery_pass.settings');
// Generate random password.
$random_password = user_password();
// Store Old Hash Password Temporarily.
if (!_recovery_pass_store_old_pass($user)) {
\Drupal::logger('recovery_pass')
->notice('Error saving old password for user @id', array(
'@id' => $user->uid,
));
}
// Save new password.
$user
->setPassword($random_password);
$user
->save();
// Retrive email body and subject.
$message = $token_service
->replace($config
->get('email_body'), array(
'user' => $user,
), $token_options);
if ($message) {
// Replace [user_new_password] placeholder with new password.
$message = str_replace("[user_new_password]", $random_password, $message);
}
$subject = $token_service
->replace($config
->get('email_subject'), array(
'user' => $user,
), $token_options);
if (\Drupal::moduleHandler()
->moduleExists("htmlmail")) {
// For html mail convert new lines to br.
$message = nl2br($message);
}
$params = array(
'body' => $message,
'subject' => $subject,
);
$langcode = $user
->getPreferredLangcode();
// Get the custom site notification email to use as the from email address
// if it has been set.
$site_mail = \Drupal::config('system.site')
->get('mail_notification');
// If the custom site notification email has not been set, we use the site
// default for this.
if (empty($site_mail)) {
$site_mail = \Drupal::config('system.site')
->get('mail');
}
if (empty($site_mail)) {
$site_mail = ini_get('sendmail_from');
}
$mail = \Drupal::service('plugin.manager.mail')
->mail('recovery_pass', 'recovery_pass', $user
->getEmail(), $langcode, $params, $site_mail);
if ($mail) {
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);
}
}