You are here

MailHandler.php in Commerce Core 8.2

Namespace

Drupal\commerce

File

src/MailHandler.php
View source
<?php

namespace Drupal\commerce;

use Drupal\commerce\Event\CommerceEvents;
use Drupal\commerce\Event\PostMailSendEvent;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class MailHandler implements MailHandlerInterface {
  use StringTranslationTrait;

  /**
   * The language default.
   *
   * @var \Drupal\Core\Language\LanguageDefault
   */
  protected $languageDefault;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The mail manager.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Constructs a new MailHandler object.
   *
   * @param \Drupal\Core\Language\LanguageDefault $language_default
   *   The language default.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   The mail manager.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(LanguageDefault $language_default, LanguageManagerInterface $language_manager, MailManagerInterface $mail_manager, EventDispatcherInterface $event_dispatcher) {
    $this->languageDefault = $language_default;
    $this->languageManager = $language_manager;
    $this->mailManager = $mail_manager;
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public function sendMail($to, $subject, array $body, array $params = []) {
    if (empty($to)) {
      return FALSE;
    }
    $default_params = [
      'headers' => [
        'Content-Type' => 'text/html; charset=UTF-8;',
        'Content-Transfer-Encoding' => '8Bit',
      ],
      'id' => 'mail',
      // The 'from' address will be set by commerce_store_mail_alter().
      'from' => '',
      'reply-to' => NULL,
      'subject' => $subject,
      'langcode' => $this->languageManager
        ->getCurrentLanguage()
        ->getId(),
      // The body will be rendered in commerce_mail(), because that's what
      // MailManager expects. The correct theme and render context aren't
      // setup until then.
      'body' => $body,
    ];
    if (!empty($params['cc'])) {
      $default_params['headers']['Cc'] = $params['cc'];
    }
    if (!empty($params['bcc'])) {
      $default_params['headers']['Bcc'] = $params['bcc'];
    }
    $params = array_replace($default_params, $params);

    // Change the active language to ensure the email is properly translated.
    if ($params['langcode'] != $default_params['langcode']) {
      $this
        ->changeActiveLanguage($params['langcode']);
    }
    $message = $this->mailManager
      ->mail('commerce', $params['id'], $to, $params['langcode'], $params, $params['reply-to']);

    // Revert back to the original active language.
    if ($params['langcode'] != $default_params['langcode']) {
      $this
        ->changeActiveLanguage($default_params['langcode']);
    }

    // Allow modules to react after an email has been sent.
    $event = new PostMailSendEvent($params, $message);
    $this->eventDispatcher
      ->dispatch(CommerceEvents::POST_MAIL_SEND, $event);
    return (bool) $message['result'];
  }

  /**
   * Changes the active language for translations.
   *
   * @param string $langcode
   *   The langcode.
   */
  protected function changeActiveLanguage($langcode) {
    if (!$this->languageManager
      ->isMultilingual()) {
      return;
    }
    $language = $this->languageManager
      ->getLanguage($langcode);
    if (!$language) {
      return;
    }

    // The language manager has no method for overriding the default
    // language, like it does for config overrides. We have to change the
    // default language service's current language.
    // @see https://www.drupal.org/project/drupal/issues/3029010
    $this->languageDefault
      ->set($language);
    $this->languageManager
      ->setConfigOverrideLanguage($language);
    $this->languageManager
      ->reset();

    // The default string_translation service, TranslationManager, has a
    // setDefaultLangcode method. However, this method is not present on
    // either of its interfaces. Therefore we check for the concrete class
    // here so that any swapped service does not break the application.
    // @see https://www.drupal.org/project/drupal/issues/3029003
    $string_translation = $this
      ->getStringTranslation();
    if ($string_translation instanceof TranslationManager) {
      $string_translation
        ->setDefaultLangcode($language
        ->getId());
      $string_translation
        ->reset();
    }
  }

}

Classes

Namesort descending Description
MailHandler