View source  
  <?php
namespace Drupal\mimemail\Plugin\Mail;
use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Mail\Plugin\Mail\PhpMail;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\mimemail\Utility\MimeMailFormatHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MimeMail extends PhpMail implements ContainerFactoryPluginInterface {
  
  protected $moduleHandler;
  
  protected $emailValidator;
  
  protected $renderer;
  
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, EmailValidatorInterface $email_validator, RendererInterface $renderer) {
    
    $this->configFactory = $config_factory;
    $this->moduleHandler = $module_handler;
    $this->emailValidator = $email_validator;
    $this->renderer = $renderer;
  }
  
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'), $container
      ->get('module_handler'), $container
      ->get('email.validator'), $container
      ->get('renderer'));
  }
  
  public function format(array $message) {
    if (is_array($message['body'])) {
      $message['body'] = implode("\n\n", $message['body']);
    }
    if (preg_match('/plain/', $message['headers']['Content-Type'])) {
      if (!($format = $this->configFactory
        ->get('mimemail.settings')
        ->get('format'))) {
        $format = filter_fallback_format();
      }
      $langcode = isset($message['langcode']) ? $message['langcode'] : '';
      $message['body'] = check_markup($message['body'], $format, $langcode);
    }
    $message = $this
      ->prepareMessage($message);
    return $message;
  }
  
  protected function prepareMessage(array $message) {
    $module = $message['module'];
    $key = $message['key'];
    $to = $message['to'];
    $from = $message['from'];
    $subject = $message['subject'];
    $body = $message['body'];
    $headers = isset($message['params']['headers']) ? $message['params']['headers'] : [];
    $plain = isset($message['params']['plain']) ? $message['params']['plain'] : NULL;
    $plaintext = isset($message['params']['plaintext']) ? $message['params']['plaintext'] : NULL;
    $attachments = isset($message['params']['attachments']) ? $message['params']['attachments'] : [];
    $site_name = $this->configFactory
      ->get('system.site')
      ->get('name');
    $site_mail = $this->configFactory
      ->get('system.site')
      ->get('mail');
    $simple_address = $this->configFactory
      ->get('mimemail.settings')
      ->get('simple_address');
    
    if (empty($from) || $from == $site_mail) {
      $mimemail_name = $this->configFactory
        ->get('mimemail.settings')
        ->get('name');
      $mimemail_mail = $this->configFactory
        ->get('mimemail.settings')
        ->get('mail');
      $from = [
        'name' => !empty($mimemail_name) ? $mimemail_name : $site_name,
        'mail' => !empty($mimemail_mail) ? $mimemail_mail : $site_mail,
      ];
    }
    if (empty($body)) {
      
      $plain = TRUE;
    }
    elseif (is_null($plain)) {
      if (is_string($to) && $this->emailValidator
        ->isValid($to)) {
        $user_plaintext_field = $this->configFactory
          ->get('mimemail.settings')
          ->get('user_plaintext_field');
        if (is_object($account = user_load_by_mail($to)) && $account
          ->hasField($user_plaintext_field)) {
          
          $plain = $account->{$user_plaintext_field}->value;
          
          $to = $account;
        }
      }
    }
    
    $subject = str_replace([
      "\n",
    ], '', trim(MailFormatHelper::htmlToText($subject)));
    $body = [
      '#theme' => 'mimemail_message',
      '#module' => $module,
      '#key' => $key,
      '#recipient' => $to,
      '#subject' => $subject,
      '#body' => $body,
    ];
    $body = $this->renderer
      ->renderPlain($body);
    
    $plain = $plain || $this->configFactory
      ->get('mimemail.settings')
      ->get('textonly');
    $from = MimeMailFormatHelper::mimeMailAddress($from);
    $mail = MimeMailFormatHelper::mimeMailHtmlBody($body, $subject, $plain, $plaintext, $attachments);
    $headers = array_merge($message['headers'], $headers, $mail['headers']);
    $message['to'] = MimeMailFormatHelper::mimeMailAddress($to, $simple_address);
    $message['from'] = $from;
    $message['subject'] = $subject;
    $message['body'] = $mail['body'];
    $message['headers'] = MimeMailFormatHelper::mimeMailHeaders($headers, $from);
    return $message;
  }
}