You are here

public static function Conversion::swiftmailer_parse_mailboxes in Swift Mailer 8.2

Same name and namespace in other branches
  1. 8 src/Utility/Conversion.php \Drupal\swiftmailer\Utility\Conversion::swiftmailer_parse_mailboxes()

Converts a string holding one or more mailboxes to an array.

Parameters

$value: A string holding one or more mailboxes.

Return value

array this return array

2 calls to Conversion::swiftmailer_parse_mailboxes()
Conversion::swiftmailer_add_mailbox_header in src/Utility/Conversion.php
Adds a mailbox header to a message.
ConversionTest::test_swiftmailer_parse_mailboxes in tests/src/Kernel/Utility/ConversionTest.php
@dataProvider swiftmailer_parse_mailboxes_dataProvider

File

src/Utility/Conversion.php, line 366

Class

Conversion
@todo

Namespace

Drupal\swiftmailer\Utility

Code

public static function swiftmailer_parse_mailboxes($value) {
  $validator = \Drupal::service('email.validator');

  // Split mailboxes by ',' (comma) and ';' (semicolon).
  $mailboxes_raw = [];
  preg_match_all("/((?:^|\\s){0,}(?:(?:\".*?\"){0,1}.*?)(?:\$|,|;))/", $value, $mailboxes_raw);

  // Define an array which will keep track of mailboxes.
  $mailboxes = [];

  // Iterate through each of the raw mailboxes and process them.
  foreach ($mailboxes_raw[0] as $mailbox_raw) {
    if (empty($mailbox_raw)) {
      continue;
    }

    // Remove leading and trailing whitespace.
    $mailbox_raw = trim($mailbox_raw);
    if (preg_match('/^.*<.*>.*$/', $mailbox_raw)) {
      $mailbox_components = explode('<', $mailbox_raw);
      $mailbox_name = trim(preg_replace("/\"/", "", $mailbox_components[0]));
      $mailbox_address = preg_replace('/>.*/', '', $mailbox_components[1]);
      if ($validator
        ->isValid($mailbox_address)) {
        $mailboxes[$mailbox_address] = $mailbox_name;
      }
    }
    else {
      $mailbox_address = preg_replace("/(,|;)/", "", $mailbox_raw);
      if ($validator
        ->isValid($mailbox_address)) {
        $mailboxes[] = $mailbox_address;
      }
    }
  }
  return $mailboxes;
}