You are here

protected function SenderAnalyzer::findSender in Mailhandler 8

Finds the message sender.

Parameters

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

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

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

File

src/Plugin/inmail/Analyzer/SenderAnalyzer.php, line 43

Class

SenderAnalyzer
Finds the sender based on "From" mail header field.

Namespace

Drupal\mailhandler\Plugin\inmail\Analyzer

Code

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

  // @todo: Support multiple addresses in https://www.drupal.org/node/2861923
  $from = $message
    ->getFrom()[0]
    ->getAddress();
  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)) {
    $user = reset($matched_users);
  }

  // Do not override a sender/account in case there is already one set.
  if (!$result
    ->getSender()) {
    $result
      ->setSender($sender);
  }
  if ($user && !$result
    ->isUserAuthenticated()) {
    $result
      ->setAccount($user);
  }
}