You are here

protected function EmailHandler::generateEmailParams in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x src/Service/EmailHandler.php \Drupal\easy_email\Service\EmailHandler::generateEmailParams()

Parameters

\Drupal\easy_email\Entity\EasyEmailInterface $email:

array $params:

Return value

array

2 calls to EmailHandler::generateEmailParams()
EmailHandler::preview in src/Service/EmailHandler.php
@inheritDoc
EmailHandler::sendEmail in src/Service/EmailHandler.php
@inheritDoc

File

src/Service/EmailHandler.php, line 298

Class

EmailHandler

Namespace

Drupal\easy_email\Service

Code

protected function generateEmailParams(EasyEmailInterface $email, $params = []) {
  $headers = [
    'Content-Transfer-Encoding' => '8Bit',
  ];
  $from = $email
    ->getFromAddress();
  if (!empty($from) && !empty($email
    ->getFromName())) {
    $from = $email
      ->getFromName() . ' <' . $from . '>';
  }
  if (!empty($from)) {
    $headers += [
      'From' => $from,
    ];
  }

  // Determine which versions of the body text we need to make
  if ($email
    ->hasField('body_html') && $email
    ->hasField('body_plain')) {
    $headers['Content-Type'] = 'text/html; charset=UTF-8;';
    if ($this
      ->shouldGeneratePlainBody($email)) {

      // We have HTML and need generate plain body text.
      $body = $this
        ->buildHtmlBody($email);

      //$body_without_inbox = $body['body'];
      $params['body'] = $body;

      //$this->renderInNewContext($body);
      $params['convert'] = TRUE;

      //$converter = new Html2Text($this->renderInNewContext($body_without_inbox));

      //$params['plain'] = trim($converter->getText());
    }
    else {

      // We have HTML and plain body text.
      $params['body'] = $this
        ->buildHtmlBody($email);

      //$this->renderInNewContext();
      $params['plain'] = $this
        ->buildPlainBody($email);

      //$this->renderInNewContext(, TRUE);
    }
  }
  elseif ($email
    ->hasField('body_html')) {

    // We have only HTML body text
    $headers['Content-Type'] = 'text/html; charset=UTF-8;';
    $params['body'] = $this
      ->buildHtmlBody($email);

    //$this->renderInNewContext();
  }
  elseif ($email
    ->hasField('body_plain')) {

    // We have only plain body text
    $headers['Content-Type'] = 'text/plain; charset=UTF-8';
    $params['body'] = $this
      ->buildPlainBody($email);

    //$this->renderInNewContext(, TRUE);
  }
  else {

    // No body: ¯\_(ツ)_/¯
  }
  $cc = $email
    ->getCCAddresses();
  if (!empty($cc)) {
    $headers['Cc'] = implode(', ', $cc);
  }
  $bcc = $email
    ->getBCCAddresses();
  if (!empty($bcc)) {
    $headers['Bcc'] = implode(', ', $bcc);
  }
  if (!empty($params['headers'])) {
    $headers += $params['headers'];
  }
  $params += [
    'headers' => $headers,
    'subject' => $email
      ->getSubject(),
  ];
  $params['easy_email'] = TRUE;
  return $params;
}