You are here

protected function FooterAnalyzer::findFooter in Mailhandler 8

Finds and returns the message footer.

Parameters

\Drupal\inmail\MIME\MimeMessageInterface $message: A mail message to be analyzed.

\Drupal\inmail\DefaultAnalyzerResult $result: The analyzer result.

1 call to FooterAnalyzer::findFooter()
FooterAnalyzer::analyze in src/Plugin/inmail/Analyzer/FooterAnalyzer.php

File

src/Plugin/inmail/Analyzer/FooterAnalyzer.php, line 47

Class

FooterAnalyzer
A message footer analyzer.

Namespace

Drupal\mailhandler\Plugin\inmail\Analyzer

Code

protected function findFooter(MimeMessageInterface $message, DefaultAnalyzerResult $result) {

  // Get a message body.
  $body = $result
    ->getBody() ?: $message
    ->getBody();
  $footer = NULL;

  // Per https://tools.ietf.org/html/rfc3676#section-4.3, footer/signature is
  // separated from the message with "-- \n".
  $body_match = preg_split('/\\s*[\\r\\n]--\\s+/', $body);
  if (count($body_match) > 1) {

    // Footer represents a string after the last occurrence of "-- \n" regex.
    $footer = end($body_match);
    $footer = trim($footer);

    // Update the analyzed body without footer.
    $footer_key = count($body_match) - 1;
    unset($body_match[$footer_key]);
    $body = implode("\n-- \n", $body_match);
    $result
      ->setBody($body);
  }
  elseif (preg_match('/On [A-Za-z]{3}, [A-Za-z]{3} [0-9]{1,2}, 20[0-9]{2} at [0-9]{1,2}:[0-9]{2} (AM|PM).+/', $body, $matches)) {
    $footer_line = reset($matches);
    $footer = strstr($body, $footer_line);
    $result
      ->setBody(strstr($body, $footer_line, TRUE));
  }
  $result
    ->setFooter($footer);
}