/**
  * {@inheritdoc}
  */
 public function mail($module, $key, $to, $langcode, $params = array(), $reply = NULL, $send = TRUE)
 {
     // Switch the theme to the configured mail theme.
     $mail_theme = $this->getMailTheme();
     $current_active_theme = $this->themeManager->getActiveTheme();
     if ($mail_theme != $current_active_theme->getName()) {
         $this->themeManager->setActiveTheme($this->themeInitialization->initTheme($mail_theme));
         // The theme registry returns the same registry object no matter which
         // theme is currently active. This works around that by having a duplicate
         // service, that is only called when the mail theme is acive.
         // @todo: This will not work if this can not be called. Remove this once
         //   https://www.drupal.org/node/2640962 is committed.
         if ($this->themeManager instanceof ThemeManager) {
             $this->themeManager->setThemeRegistry($this->mailThemeRegistry);
         }
     }
     try {
         $message = parent::mail($module, $key, $to, $langcode, $params, $reply, $send);
     } finally {
         // Revert the active theme, this is done inside a finally block so it is
         // executed even if an exception is thrown during sending a mail.
         if ($mail_theme != $current_active_theme->getName()) {
             $this->themeManager->setActiveTheme($current_active_theme);
             if ($this->themeManager instanceof ThemeManager) {
                 $this->themeManager->setThemeRegistry($this->defaultThemeRegistry);
             }
         }
     }
     return $message;
 }
 /**
  * {@inheritdoc}
  */
 public function createInstance($plugin_id, array $configuration = [])
 {
     $instance = parent::createInstance($plugin_id, $configuration);
     $wrapper = new MailPluginWrapper($instance, $this->dataCollector, $plugin_id, $configuration);
     return $wrapper;
 }