You are here

class MailHandler in Commerce Core 8.2

Hierarchy

Expanded class hierarchy of MailHandler

1 string reference to 'MailHandler'
commerce.services.yml in ./commerce.services.yml
commerce.services.yml
1 service uses MailHandler
commerce.mail_handler in ./commerce.services.yml
Drupal\commerce\MailHandler

File

src/MailHandler.php, line 14

Namespace

Drupal\commerce
View source
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();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MailHandler::$eventDispatcher protected property The event dispatcher.
MailHandler::$languageDefault protected property The language default.
MailHandler::$languageManager protected property The language manager.
MailHandler::$mailManager protected property The mail manager.
MailHandler::changeActiveLanguage protected function Changes the active language for translations.
MailHandler::sendMail public function Sends an email to a user. Overrides MailHandlerInterface::sendMail
MailHandler::__construct public function Constructs a new MailHandler object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.