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()->query->get('mail');
  $database = \Drupal::database();
  $username = $database->query("SELECT name from users_field_data where mail='$mail'")->fetchField();
  if (!empty($username)) {
    $module = 'modulename';
    $key = 'forgotusernamerecovery';
    $to = '[email protected]';
    $langcode = '';
    $params = ['username' => 'Abc'];
    drupal_mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE);
    $message = "<div class='matched'>Successs</div>";
  else {
    $message = "<div class='notmatched'>notmatched</div>";
  $response = new AjaxResponse();
  $response->addCommand(new HtmlCommand('.selector', $message));
  return $response;
  • I’m voting to close this question because it's asking for recommendations or suggestions on books, tools, modules, themes, distributions, tutorials, or other off-site resources.
    – apaderno
    Commented Jan 28, 2021 at 22:37

3 Answers 3


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.


Swiftmailer in combination withmail system let you send html mail if correctly configured. It's decently documented, the very short guide is:

  1. go to: admin/config/system/mailsystem, set as formatter and sender swift mailer, use the front\whatever theme as theme for mail
  2. (optional) set the formatter\sender for the specific module
  3. go to admin/config/swiftmailer/messages check the configuration, the default should already work for html mails.

You could then override the opportune template twig to style your email. You can also definy a css file that swiftmailer will convert in inline-css. Check the readme for more details but in short you have to define inside the your_theme.libraries.yml file

      css/my_theme.mail.css: {}

You can send emails using the MailManager class. If you are in the context of an injectable class I would strongly suggest to use dependency injection.

If now you can simply call the service like so:

\Drupal::service('plugin.manager.mail')->mail($module, $key, $to, $langcode, $params = array(), $reply = NULL, $send = TRUE);

In short just change drupal_mail(...) to \Drupal::service('plugin.manager.mail')->mail(...) and i believe that the above code will work as you intend it to.

Edit: Also make sure that in your .module file there is a hook like the one below.

Edit 2: Also remember to add subject and body in your params. And correct your function call, because as it is now you overwrite the $params to an empty array when you call the function.

 * Implements hook_mail().
function MYMODULE_mail($key, &$message, $params) {
  $site_name = \Drupal::config('system.site')->get('name');
  $site_mail = \Drupal::config('system.site')->get('mail');

  switch ($key) {
    case 'my_module_key':
      $message['headers']['Reply-To'] = $site_mail;
      $message['headers']['From'] = $site_name . '< ' . $site_mail . ' >';
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['body'];

This hook will manipulate the data to be used for the construction of the mail and is mandatory for the mail service to work.

Caution: the $key you use must be the same with the $key provided in the mail function call and the modulename.. well should be obvious :P

  • Thanks for the reply. I face two issue with this (1) Error Type SMTP --- Error sending email (from [email protected] to [email protected] : Message body empty (2) Error Type mail -- Mail : Error sending email (from [email protected] to [email protected] with reply-to not set).
    – user102249
    Commented Jan 27, 2021 at 15:31
  • When you try to use the mail service via a module you should have a hook_module in your .module file, which will manipulate some variables. I will edit my post to provide specifications. Commented Jan 27, 2021 at 15:38
  • Check the edited answer, and tell me if you are still getting errors. Commented Jan 27, 2021 at 15:48
  • TypeError: Argument 3 passed to t() must be of the type array, null given, called in
    – user102249
    Commented Jan 27, 2021 at 16:04
  • Check the updated version of the question. Commented Jan 27, 2021 at 17:37