Sending emails

We wanted to use our MailLogger to send out an email whenever we are logging an error. So let's go back to our class and add this logic.

This is what our log() method can look like:

  /**   * {@inheritdoc}   */  public function log($level, $message, array $context = array()) {    if ($level !== RfcLogLevel::ERROR) {      return;    }    $to = $this->configFactory->get('system.site')->get('mail');    $langode = $this->configFactory->get('system.site')->get('langcode');    $variables = $this->parser->parseMessagePlaceholders($message, $context);    $markup = new FormattableMarkup($message, $variables);    \Drupal::service('plugin.manager.mail')->mail('hello_world', 'hello_world_log', $to, $langode, ['message' => $markup]);  }

First of all, we stated that we ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.