You are here

protected function PGPAnalyzer::findSender in Mailhandler 8

Finds the sender from given mail message.

Parameters

\Drupal\inmail\MIME\MimeMessageInterface $message: The mail message.

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

array $context: The array with context data.

Throws

\Exception Throws an exception if user is not authenticated.

1 call to PGPAnalyzer::findSender()
PGPAnalyzer::analyze in src/Plugin/inmail/Analyzer/PGPAnalyzer.php

File

src/Plugin/inmail/Analyzer/PGPAnalyzer.php, line 249

Class

PGPAnalyzer
An analyzer for PGP signed messages.

Namespace

Drupal\mailhandler\Plugin\inmail\Analyzer

Code

protected function findSender(MimeMessageInterface $message, DefaultAnalyzerResult $result, array &$context) {
  $sender = NULL;
  $user = NULL;
  $matches = [];

  // @todo: Support multiple addresses in https://www.drupal.org/node/2861923
  $from = $message
    ->getFrom()[0]
    ->getAddress();

  // Use signed headers to extract "from" address for PGP/MIME messages.
  if ($result
    ->getContext('pgp')
    ->getContextValue()['pgp_type'] == 'mime') {

    /** @var \Drupal\inmail\MIME\MimeMultipartEntity $message */
    $signed_text_part = $message
      ->getPart($context['signed_text_index']);
    $from = $signed_text_part
      ->getHeader()
      ->getFieldBody('From') ?: $from;
  }
  preg_match('/[^@<\\s]+@[^@\\s>]+/', $from, $matches);
  if (!empty($matches)) {
    $sender = reset($matches);
    $matched_users = \Drupal::entityTypeManager()
      ->getStorage('user')
      ->loadByProperties([
      'mail' => $sender,
    ]);
    if (!empty($matched_users)) {
      $result
        ->setAccount(reset($matched_users));
    }
  }
  $result
    ->setSender($sender);
  if (!$result
    ->isUserAuthenticated()) {
    throw new \Exception('User is not authenticated.');
  }
}