You are here

protected function SendGridMail::removeHeaders in SendGrid Integration 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Mail/SendGridMail.php \Drupal\sendgrid_integration\Plugin\Mail\SendGridMail::removeHeaders()

Strips the headers from the body part.

Parameters

string $input: A string containing the body part to strip.

Return value

string A string with the stripped body part.

1 call to SendGridMail::removeHeaders()
SendGridMail::mail in src/Plugin/Mail/SendGridMail.php
Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail().

File

src/Plugin/Mail/SendGridMail.php, line 610
Implements Drupal MailSystemInterface.

Class

SendGridMail
@file Implements Drupal MailSystemInterface.

Namespace

Drupal\sendgrid_integration\Plugin\Mail

Code

protected function removeHeaders($input) {
  $part_array = explode("\n", $input);

  // Will strip these headers according to RFC2045.
  $headers_to_strip = [
    'Content-Type',
    'Content-Transfer-Encoding',
    'Content-ID',
    'Content-Disposition',
  ];
  $pattern = '/^(' . implode('|', $headers_to_strip) . '):/';
  while (count($part_array) > 0) {

    // Ignore trailing spaces/newlines.
    $line = rtrim($part_array[0]);

    // If the line starts with a known header string.
    if (preg_match($pattern, $line)) {
      $line = rtrim(array_shift($part_array));

      // Remove line containing matched header.
      // If line ends in a ';' and the next line starts with four spaces, it's a continuation
      // of the header split onto the next line. Continue removing lines while we have this condition.
      while (substr($line, -1) == ';' && count($part_array) > 0 && substr($part_array[0], 0, 4) == '    ') {
        $line = rtrim(array_shift($part_array));
      }
    }
    else {

      // No match header, must be past headers; stop searching.
      break;
    }
  }
  $output = implode("\n", $part_array);
  return $output;
}