You are here

class SenderAnalyzer in Mailhandler 8

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

This analyzer extracts the email address from "From" mail header field and based on this information finds the corresponding user. As this option is not entirely safe, it is disabled by default.

Plugin annotation


@Analyzer(
  id = "sender",
  label = @Translation("User Sender Analyzer")
)

Hierarchy

  • class \Drupal\mailhandler\Plugin\inmail\Analyzer\SenderAnalyzer extends \Drupal\inmail\Plugin\inmail\Analyzer\AnalyzerBase

Expanded class hierarchy of SenderAnalyzer

File

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

Namespace

Drupal\mailhandler\Plugin\inmail\Analyzer
View source
class SenderAnalyzer extends AnalyzerBase {

  /**
   * {@inheritdoc}
   */
  public function analyze(MimeMessageInterface $message, ProcessorResultInterface $processor_result) {
    $result = $processor_result
      ->getAnalyzerResult();
    $this
      ->findSender($message, $result);
  }

  /**
   * Finds the message sender.
   *
   * @param \Drupal\inmail\MIME\MimeMessageInterface $message
   *   The mail message.
   * @param \Drupal\inmail\DefaultAnalyzerResult $result
   *   The analyzer result.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SenderAnalyzer::analyze public function
SenderAnalyzer::findSender protected function Finds the message sender.