You are here

public function MailManager::mail in Easy Email 2.0.x

Same name and namespace in other branches
  1. 8 modules/easy_email_override/src/Service/MailManager.php \Drupal\easy_email_override\Service\MailManager::mail()

@inheritDoc

Overrides MailManagerInterface::mail

File

modules/easy_email_override/src/Service/MailManager.php, line 49

Class

MailManager
Class MailManager.

Namespace

Drupal\easy_email_override\Service

Code

public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
  $email_handler = \Drupal::service('easy_email.handler');

  /** @var \Drupal\easy_email_override\Entity\EmailOverrideInterface[] $email_overrides */
  $email_overrides = \Drupal::entityTypeManager()
    ->getStorage('easy_email_override')
    ->loadByProperties([
    'module' => $module,
    'key' => $key,
  ]);
  if (!empty($email_overrides)) {

    // If we find more than one override for a given module/key combo, we'll send them all.
    // Not sure if that will be useful, but perhaps.
    foreach ($email_overrides as $email_override) {
      $email = $email_handler
        ->createEmail([
        'type' => $email_override
          ->getEasyEmailType(),
      ]);
      $param_map = $email_override
        ->getParamMap();
      foreach ($param_map as $pm) {
        $email
          ->set($pm['destination'], $params[$pm['source']]);
      }
      $result = $email_handler
        ->sendEmail($email);
      $send = FALSE;
    }
  }
  $message = $this->decorated
    ->mail($module, $key, $to, $langcode, $params, $reply, $send);
  if (!isset($message['result']) && !empty($email_overrides)) {
    $message['result'] = TRUE;
  }
  return $message;
}