You are here

function swiftmailer_parse_mailboxes in Swift Mailer 7

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

Parameters

$value: A string holding one or more mailboxes.

2 calls to swiftmailer_parse_mailboxes()
swiftmailer_add_mailbox_header in includes/helpers/conversion.inc
Adds a mailbox header to a message.
SWIFTMailSystem::mail in includes/classes/SWIFTMailSystem.inc
Sends a message composed by drupal_mail().

File

includes/helpers/conversion.inc, line 354
This file contains conversion functions.

Code

function swiftmailer_parse_mailboxes($value) {

  // Split mailboxes by ',' (comma) and ';' (semicolon).

  //$mailboxes_raw = preg_split('/(,|;)/', $value);
  $mailboxes_raw = array();
  preg_match_all("/((?:^|\\s){0,}(?:(?:\".*?\"){0,1}.*?)(?:\$|,|;))/", $value, $mailboxes_raw);

  // Define an array which will keep track of mailboxes.
  $mailboxes = array();

  // 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 (valid_email_address($mailbox_address)) {
        $mailboxes[$mailbox_address] = $mailbox_name;
      }
    }
    else {
      $mailbox_address = preg_replace("/(,|;)/", "", $mailbox_raw);
      if (valid_email_address($mailbox_address)) {
        $mailboxes[] = $mailbox_address;
      }
    }
  }
  return $mailboxes;
}