You are here

function monitoring_mail_mail in Monitoring 8

Implements hook_mail().

Send monitoring result status transition mail.

File

modules/monitoring_mail/monitoring_mail.module, line 18
Monitoring Mail bootstrap file.

Code

function monitoring_mail_mail($key, &$message, $params) {

  // Get values to build the mail data.

  /** @var \Drupal\monitoring\Result\SensorResultInterface $result */
  $result = $params['result'];

  /** @var \Drupal\monitoring\SensorConfigInterface $sensor_config */
  $sensor_config = $params['sensor_config'];
  $site_config = \Drupal::config('system.site');
  $site_mail = $site_config
    ->get('mail');
  $site_name = $site_config
    ->get('name');
  $status_old = $params['status_old'];
  $status_new = $params['status_new'];
  $status = $result
    ->getStatusLabel();
  $storage = \Drupal::keyValue('monitoring_mail_key');

  // Set the mail subject.
  $subject = strtoupper($status) . ': ' . $result
    ->getValue() . ' ' . $sensor_config
    ->getLabel();
  $message['subject'] = $subject;

  // Set the mail body.
  $body = [];
  $body[] = $result
    ->getMessage();
  $body[] = '';
  $url = Url::fromRoute('entity.monitoring_sensor_config.details_form', [
    'monitoring_sensor_config' => $result
      ->getSensorId(),
  ]);
  $body[] = 'Sensor: ' . $url
    ->toString();
  $body[] = '';
  $body[] = 'Status: ' . $status_old . ' > ' . $status_new;
  $body[] = '';
  $message['body'] = $body;

  // Set the mail sender.
  $message['from'] = 'MONITORING ' . $site_name . ' <' . $site_mail . '>';

  // @todo Set header to build a per-sensor thread.
  // Set the mail header.
  $message['headers']['From'] = $message['from'];

  // Set the header Message-ID.
  $message_id = '<' . $result
    ->getSensorId() . '.' . time() . '@' . \Drupal::request()
    ->getHost() . '>';
  $message['headers']['Message-ID'] = $message_id;

  // Set the header References, if there is a previous transition.
  if ($references = $storage
    ->get($result
    ->getSensorId())) {
    $message['headers']['References'] = $references;
  }

  // Store the Message-ID of the current sensor transition.
  $storage
    ->set($result
    ->getSensorId(), $message_id);
}