How can I send an HTML email from an Ajax callback? I want to send an email to the user who forget username.
I created a functionality that handle with Ajax like send request to controller where I using a query to check whether user exist or not. if user exist I want to send email to used with his user name.
Is there any module that i can use to send email in this way
public function ForgotUsername() {
  $mail = \Drupal::request()-&gt;query-&gt;get('mail');
  $database = \Drupal::database();
  $username = $database-&gt;query(&quot;SELECT name from users_field_data where mail='$mail'&quot;)-&gt;fetchField();
  
  if (!empty($username)) {
    $module = 'modulename';
    $key = 'forgotusernamerecovery';
    $to = 'example@example.com';
    $langcode = '';
    $params = ['username' =&gt; 'Abc'];
    drupal_mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE);
    
    $message = &quot;&lt;div class='matched'&gt;Successs&lt;/div&gt;&quot;;
  }
  else {
    $message = &quot;&lt;div class='notmatched'&gt;notmatched&lt;/div&gt;&quot;;
  }
  $response = new AjaxResponse();
  $response-&gt;addCommand(new HtmlCommand('.selector', $message));
  return $response;
}
I recommend the Easy Email module. It combines with Swiftmailer and Mailsystem, allowing for creating and sending HTML emails, using templates you can build with a ckeditor right in the interface. Then you can send mails from your module using the standard Drupal mail service, and it will take care of turning it into an HTML email. The video on the top page of the module page is very informative and explains it all. It's by far the best HTML email solution I've found in Drupal over the years.