View source  
  <?php
namespace Drupal\htmlmail\Plugin\Mail;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Mail\MailInterface;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Mail;
use Drupal\Core\Render\Renderer;
use Drupal\Component\Utility\Unicode;
use Drupal\htmlmail\Helper\HtmlMailHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Egulias\EmailValidator\EmailValidator;
use Drupal\htmlmail\Utility\HTMLMailMime;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
class HTMLMailSystem implements MailInterface, ContainerFactoryPluginInterface {
  protected $emailValidator;
  protected $systemConfig;
  protected $moduleHandler;
  protected $logger;
  protected $configVariables;
  protected $siteSettings;
  protected $fileSystem;
  protected $renderer;
  protected $mimeType;
  
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EmailValidator $emailValidator, ModuleHandlerInterface $moduleHandler, FileSystemInterface $fileSystem, LoggerChannelFactoryInterface $logger, Settings $settings, Renderer $renderer, MimeTypeGuesserInterface $mimeTypeGuesser) {
    $this->emailValidator = $emailValidator;
    $this->moduleHandler = $moduleHandler;
    $this->fileSystem = $fileSystem;
    $this->logger = $logger;
    $this->systemConfig = \Drupal::config('system.site');
    $this->configVariables = \Drupal::config('htmlmail.settings');
    $this->siteSettings = $settings;
    $this->renderer = $renderer;
    $this->mimeType = $mimeTypeGuesser;
  }
  
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('email.validator'), $container
      ->get('module_handler'), $container
      ->get('file_system'), $container
      ->get('logger.factory'), $container
      ->get('settings'), $container
      ->get('renderer'), $container
      ->get('file.mime_type.guesser'));
  }
  
  public function getLogger() {
    return $this->logger
      ->get('htmlmail');
  }
  
  public function getDefaultFromMail() {
    $site_mail = $this->systemConfig
      ->get('mail');
    return $site_mail ?: ini_get('sendmail_from');
  }
  
  public function getDefaultSiteName() {
    $site_name = $this->systemConfig
      ->get('site_name');
    return $site_name ?: 'Drupal';
  }
  
  public function format(array $message) {
    $eol = $this->siteSettings
      ->get('mail_line_endings', PHP_EOL);
    $default_from = $this
      ->getDefaultFromMail();
    $force_plain = $this->configVariables
      ->get('htmlmail_html_with_plain');
    if (!empty($message['headers']['From']) && $message['headers']['From'] == $default_from && $this->emailValidator
      ->isValid($default_from)) {
      $message['headers']['From'] = '"' . str_replace('"', '', $this
        ->getDefaultSiteName()) . '" <' . $default_from . '>';
    }
    
    if ($this->configVariables
      ->get('htmlmail_use_mime_mail')) {
      $body = $this
        ->formatMailMime($message);
      $plain = $message['MailMIME']
        ->getTXTBody();
    }
    else {
      
      if (is_array($message['body'])) {
        
        $message['body'] = implode("{$eol}{$eol}", $message['body']);
        
        $message['body'] = MailFormatHelper::htmlToText($message['body']);
        
        $message['body'] = MailFormatHelper::wrapMail($message['body']);
      }
      $theme = [
        '#theme' => HtmlMailHelper::getThemeNames($message),
        '#message' => $message,
      ];
      $body = $this->renderer
        ->render($theme);
      if ($message['body'] && !$body) {
        $this
          ->getLogger()
          ->warning('The %theme function did not return any text.  Please check your template file for errors.', [
          '%theme' => "Drupal::service('renderer')->render([\$theme])",
        ]);
        $body = $message['body'];
      }
      $plain = MailFormatHelper::htmlToText($body);
      if ($body && !$plain) {
        $this
          ->getLogger()
          ->warning('The %convert function did not return any text. Please report this error to the %mailsystem issue queue.', [
          '%convert' => 'MailFormatHelper::htmlToText()',
          '%mailsystem' => 'Mail system',
        ]);
      }
    }
    
    if ($body && HtmlMailHelper::htmlMailIsAllowed($message['to']) && !$force_plain) {
      
      if ($this->moduleHandler
        ->moduleExists('echo') && ($theme = HtmlMailHelper::getSelectedTheme($message))) {
        $themed_body = echo_themed_page($message['subject'], $body, $theme);
        if ($themed_body) {
          $body = $themed_body;
        }
        else {
          $this
            ->getLogger()
            ->warning('The %echo function did not return any text. Please check the page template of your %theme theme for errors.', [
            '%echo' => 'echo_themed_page()',
            '%theme' => $theme,
          ]);
        }
      }
      
      if ($filter = $this->configVariables
        ->get('htmlmail_postfilter')) {
        $filtered_body = check_markup($body, $filter);
        if ($filtered_body) {
          $body = $filtered_body;
        }
        else {
          $this
            ->getLogger()
            ->warning('The %check function did not return any text. Please check your %filter output filter for errors.', [
            '%check' => 'check_markup()',
            '%filter' => $filter,
          ]);
        }
      }
      
      if (isset($message['MailMIME'])) {
        $mime =& $message['MailMIME'];
        $mime
          ->setHTMLBody($body);
        if (isset($message['params']['attachments'])) {
          foreach ($message['params']['attachments'] as $attachment) {
            $mime
              ->addAttachment($this->fileSystem
              ->realpath($attachment['uri']), $attachment['filemime'], $attachment['filename'], TRUE, 'base64', 'attachment', 'UTF-8', '', '');
          }
        }
        list($message['headers'], $message['body']) = $mime
          ->toEmail($message['headers']);
        if (!$message['body']) {
          $this
            ->getLogger()
            ->warning('The %toemail function did not return any text. Please report this error to the %mailmime issue queue.', [
            '%toemail' => 'HTMLMailMime::toEmail()',
            '%mailmime' => 'Mail MIME',
          ]);
        }
      }
      else {
        $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
        $message['body'] = $body;
        if ($this->configVariables
          ->get('htmlmail_html_with_plain')) {
          $boundary = uniqid('np');
          $message['headers']['Content-Type'] = 'multipart/alternative;boundary="' . $boundary . '"';
          $html = $message['body'];
          $raw_message = 'This is a MIME encoded message.';
          $raw_message .= $eol . $eol . "--" . $boundary . $eol;
          $raw_message .= "Content-Type: text/plain;charset=utf-8" . $eol . $eol;
          $raw_message .= MailFormatHelper::htmlToText($html);
          $raw_message .= $eol . $eol . "--" . $boundary . $eol;
          $raw_message .= "Content-Type: text/html;charset=utf-8" . $eol . $eol;
          $raw_message .= $html;
          $raw_message .= $eol . $eol . "--" . $boundary . "--";
          $message['body'] = $raw_message;
        }
      }
    }
    else {
      if (isset($message['MailMIME'])) {
        $mime =& $message['MailMIME'];
        $mime
          ->setHTMLBody('');
        $mime
          ->setContentType('text/plain', [
          'charset' => 'utf-8',
        ]);
        if (isset($message['params']['attachments'])) {
          foreach ($message['params']['attachments'] as $attachment) {
            $mime
              ->addAttachment($this->fileSystem
              ->realpath($attachment['uri']), $attachment['filemime'], $attachment['filename'], TRUE, 'base64', 'attachment', 'UTF-8', '', '');
          }
        }
        list($message['headers'], $message['body']) = $mime
          ->toEmail($message['headers']);
        if (!$message['body']) {
          $this
            ->getLogger()
            ->warning('The %toemail function did not return any text. Please report this error to the %mailmime issue queue.', [
            '%toemail' => 'HTMLMailMime::toEmail()',
            '%mailmime' => 'Mail MIME',
          ]);
        }
      }
      else {
        $message['body'] = $plain;
        $message['headers']['Content-Type'] = 'text/plain; charset=utf-8';
      }
    }
    return $message;
  }
  
  public function mail(array $message) {
    $eol = $this->siteSettings
      ->get('mail_line_endings', PHP_EOL);
    $params = [];
    
    $message += [
      'subject' => t('(No subject)'),
    ];
    
    if (empty($message['to'])) {
      if (empty($message['headers']['To'])) {
        $this
          ->getLogger()
          ->error('Cannot send email about %subject without a recipient.', [
          '%subject' => $message['subject'],
        ]);
        return FALSE;
      }
      $message['to'] = $message['headers']['To'];
    }
    if (class_exists('HTMLMailMime')) {
      $mime = new HTMLMailMime($this->logger, $this->siteSettings, $this->mimeType, $this->fileSystem);
      $to = $mime
        ->mimeEncodeHeader('to', $message['to']);
      $subject = $mime
        ->mimeEncodeHeader('subject', $message['subject']);
      $txt_headers = $mime
        ->mimeTxtHeaders($message['headers']);
    }
    else {
      $to = Unicode::mimeHeaderEncode($message['to']);
      $subject = Unicode::mimeHeaderEncode($message['subject']);
      $txt_headers = $this
        ->txtHeaders($message['headers']);
    }
    $body = preg_replace('#(\\r\\n|\\r|\\n)#s', $eol, $message['body']);
    
    if (empty($body)) {
      $this
        ->getLogger()
        ->warning('Refusing to send a blank email to %recipient about %subject.', [
        '%recipient' => $message['to'],
        '%subject' => $message['subject'],
      ]);
      return FALSE;
    }
    if ($this->configVariables
      ->get('htmlmail_debug')) {
      $params = [
        $to,
        $subject,
        Unicode::substr($body, 0, min(80, strpos("\n", $body))) . '...',
        $txt_headers,
      ];
    }
    if (isset($message['headers']['Return-Path'])) {
      
      if (isset($_SERVER['WINDIR']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) {
        
        $old_from = ini_get('sendmail_from');
        ini_set('sendmail_from', $message['headers']['Return-Path']);
        $result = @mail($to, $subject, $body, $txt_headers);
        ini_set('sendmail_from', $old_from);
      }
      elseif (ini_get('safe_mode')) {
        
        $result = @mail($to, $subject, $body, $txt_headers);
      }
      else {
        
        $extra = '-f' . $message['headers']['Return-Path'];
        $result = @mail($to, $subject, $body, $txt_headers, $extra);
        if ($this->configVariables
          ->get('htmlmail_debug')) {
          $params[] = $extra;
        }
      }
    }
    else {
      
      $result = @mail($to, $subject, $body, $txt_headers);
    }
    if (!$result && $this->configVariables
      ->get('htmlmail_debug')) {
      $call = '@mail(' . implode(', ', $params) . ')';
      foreach ($params as $i => $value) {
        $params[$i] = var_export($value, 1);
      }
      if (defined('DEBUG_BACKTRACE_IGNORE_ARGS')) {
        $trace = print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 1);
      }
      else {
        $trace = debug_backtrace(0);
        for ($i = count($trace) - 1; $i >= 0; $i--) {
          unset($trace[$i]['args']);
        }
        $trace = print_r($trace);
      }
      $this
        ->getLogger()
        ->info('Mail sending failed because:<br /><pre>@call</pre><br />returned FALSE.<br /><pre>@trace</pre>', [
        '@call' => $call,
        '@trace' => $trace,
      ]);
    }
    return $result;
  }
  
  public function formatMailMime(array &$message) {
    $eol = $this->siteSettings
      ->get('mail_line_endings', PHP_EOL);
    $message['body'] = HTMLMailMime::concat($message['body'], $eol);
    
    $email = HTMLMailMime::encodeEmail($message['headers'], $message['body'], $eol);
    
    if (!($mime = HTMLMailMime::parse($email, $this->logger, $this->mimeType, $this->fileSystem))) {
      $this
        ->getLogger()
        ->error('Could not parse email message.');
      return $message;
    }
    
    $email = $message;
    if (!($email['body'] = $mime
      ->getHtmlBody()) && !($email['body'] = $mime
      ->getTxtBody())) {
      $email['body'] = '';
    }
    else {
      
      if ($email['body'] === strip_tags($email['body']) && preg_match('/.' . $eol . './', $email['body'])) {
        
        $email['body'] = '<pre>' . $email['body'] . '</pre>';
      }
    }
    
    $theme = [
      '#theme' => HtmlMailHelper::getThemeNames($message),
      '#message' => $email,
    ];
    $body = $this->renderer
      ->render($theme);
    $mime
      ->setHtmlBody($body);
    $mime
      ->setTxtBody(MailFormatHelper::htmlToText($body));
    $message['MailMIME'] =& $mime;
    return $body;
  }
  
  public function txtHeaders(array $headers) {
    $output = [];
    foreach ($headers as $name => $value) {
      if (is_array($value)) {
        foreach ($value as $val) {
          $output[] = "{$name}: {$val}";
        }
      }
      else {
        $output[] = "{$name}: {$value}";
      }
    }
    return implode("\n", $output);
  }
}