You are here

protected function SMTPMailSystem::getComponents in SMTP Authentication Support 8

Returns an array of name and email address from a string.

Parameters

string $input: A string that contains different possible combinations of names and email address.

Return value

array An array containing a name and an email address.

2 calls to SMTPMailSystem::getComponents()
SMTPMailSystem::mail in src/Plugin/Mail/SMTPMailSystem.php
Send the e-mail message.
SMTPMailSystemTestHelper::publicGetComponents in tests/src/Unit/Plugin/Mail/SMTPMailSystemTest.php
Exposes getComponents for testing.

File

src/Plugin/Mail/SMTPMailSystem.php, line 792

Class

SMTPMailSystem
Modify the drupal mail system to use smtp when sending emails.

Namespace

Drupal\smtp\Plugin\Mail

Code

protected function getComponents($input) {
  $input = trim($input);
  $components = [
    'input' => $input,
    'name' => '',
    'email' => '',
  ];

  // If the input is a valid email address in its entirety,
  // then there is nothing to do, just return that.
  if ($this->emailValidator
    ->isValid($input)) {
    $components['email'] = $input;
    return $components;
  }

  // Check if $input has one of the following formats, extract what we can:
  // some name <address@example.com>.
  // "another name" <address@example.com>.
  // <address@example.com>.
  if (preg_match('/^"?([^"\\t\\n]*)"?\\s*<([^>\\t\\n]*)>$/', $input, $matches)) {
    $components['name'] = trim($matches[1]);
    $components['email'] = trim($matches[2]);
  }
  return $components;
}