O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing hook_mail()

As I mentioned earlier, the first step for sending mails in Drupal 8 is implementing hook_mail(). In our case, it can look something like this:

/** * Implements hook_mail(). */function hello_world_mail($key, &$message, $params) {  switch ($key) {    case 'hello_world_log':      $message['from'] = \Drupal::config('system.site')->get('mail');      $message['subject'] = t('There is an error on your website');      $message['body'][] = $params['message'];      break;  }}

This hook receives three parameters--the message key (template) that is used to send the mail, the message of the email that needs to be filled in, and an array of parameters passed from the client code. As you can see, we are defining a key (or template) named hello_world_log ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required