Skip to content
Snippets Groups Projects
Commit ae707c58 authored by Scott Euser's avatar Scott Euser
Browse files

Issue #3408649 by scott_euser, Dikshi Suthar, luxx91: OTP Email not send

parent def1e531
No related branches found
No related tags found
1 merge request!18Resolve #3408649 "Otp email does not send"
......@@ -5,6 +5,7 @@
* Create reset password email OTP functions.
*/
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Routing\RouteMatchInterface;
/**
......@@ -32,3 +33,23 @@ function reset_password_email_otp_help($route_name, RouteMatchInterface $route_m
return $output;
}
}
/**
* Implements hook_mail();
*/
function reset_password_email_otp_mail($key, &$message, $params) {
$token_service = \Drupal::token();
$language_manager = \Drupal::languageManager();
$langcode = $message['langcode'];
$variables = ['user' => $params['account']];
$language = $language_manager->getLanguage($langcode);
$original_language = $language_manager->getConfigOverrideLanguage();
$language_manager->setConfigOverrideLanguage($language);
$token_options = ['langcode' => $langcode, 'callback' => 'user_mail_tokens', 'clear' => TRUE];
$message['subject'] .= PlainTextOutput::renderFromHtml($token_service->replace($params['subject'], $variables, $token_options));
$message['body'][] = $token_service->replace($params['message'], $variables, $token_options);
$language_manager->setConfigOverrideLanguage($original_language);
}
......@@ -142,26 +142,29 @@ class EmailPassOTPContents {
/**
* Send mail callback.
*/
public function resetPasswordEmailOtpSend($mail, $otp, $key_mail) {
public function resetPasswordEmailOtpSend($mail, $otp, $key_mail, AccountInterface $account): void {
$module = 'reset_password_email_otp';
switch ($key_mail) {
case 'reset-password-email-otp':
// Get Admin email id.
$params['message'] = str_replace('[OTP]', $otp, $this->configFactory->getEditable('reset_password_email_otp.settings')
->get('reset_password_email_otp_mail_body'));
$settings = $this->configFactory->get('reset_password_email_otp.settings');
$params['account'] = $account;
$params['subject'] = $settings->get('reset_password_email_otp_mail_subject');
$params['message'] = str_replace('[OTP]', $otp, $settings->get('reset_password_email_otp_mail_body'));
$langcode = $this->account->getPreferredLangcode();
$send = TRUE;
// Send mail to all user.
$to = $mail;
if (!empty($to)) {
$result = $this->mailManager->mail($module, $key_mail, $to, $langcode, $params, NULL, $send);
}
if ($result['result'] != TRUE) {
$message = $this->t('There was a problem sending your email notification to @email.', ['@email' => 'users']);
$this->messenger->addError($message);
$this->loggerFactory->error($message);
return;
if ($result['result'] !== TRUE) {
$message = $this->t('There was a problem sending your email notification to @email.', ['@email' => 'users']);
$this->messenger->addError($message);
$this->loggerFactory->error($message);
return;
}
}
break;
......
......@@ -163,7 +163,7 @@ class EmailOTPCheck extends FormBase {
// Generate OTP and save in DB for every reset override old one.
$this->emailPassOTPContents->sendUserResetPasswordOtpWithEmail($account->id(), $otp);
// Send mail with OTP.
$this->emailPassOTPContents->resetPasswordEmailOtpSend($account->get('mail')->value, $otp, 'reset-password-email-otp');
$this->emailPassOTPContents->resetPasswordEmailOtpSend($account->get('mail')->value, $otp, 'reset-password-email-otp', $account);
}
}
// Make sure the status text is displayed even if no email was sent. This
......
......@@ -56,14 +56,14 @@ class ResetPasswordMailOTPConfig extends ConfigFormBase {
$form['reset_password_email_otp_mail_subject'] = [
'#title' => $this->t('Reset Mail OTP mail subject'),
'#type' => 'textfield',
'#description' => $this->t('Reset Password Mail OTP mail subject content.'),
'#description' => $this->t("Reset Password Mail OTP mail subject content.<br>The following tokens are supported: [user:display-name], [user:account-name], [user:mail]"),
'#default_value' => $config->get('reset_password_email_otp_mail_subject'),
];
$form['reset_password_email_otp_mail_body'] = [
'#title' => $this->t('Reset Password Mail OTP mail body'),
'#type' => 'textarea',
'#description' => $this->t('Make sure to use [OTP] in email body for contains OTP value.'),
'#description' => $this->t("Make sure to use [OTP] in email body for contains OTP value.<br>The following tokens are supported: [user:display-name], [user:account-name], [user:mail]"),
'#default_value' => $config->get('reset_password_email_otp_mail_body'),
];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment