You are here

public static function Conversion::swiftmailer_add_parameterized_header in Swift Mailer 8

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

Adds a parameterized header to a message.

Parameters

\Swift_Message $message: The message which the parameterized header is to be added to.

string $key: The header key.

string $value: The header value.

1 call to Conversion::swiftmailer_add_parameterized_header()
SwiftMailer::mail in src/Plugin/Mail/SwiftMailer.php
Sends a message composed by drupal_mail().

File

src/Utility/Conversion.php, line 108

Class

Conversion
@todo

Namespace

Drupal\swiftmailer\Utility

Code

public static function swiftmailer_add_parameterized_header(Swift_Message $message, $key, $value) {

  // Remove any already existing header identified by the provided key.
  static::swiftmailer_remove_header($message, $key);

  // Define variables to hold the header's value and parameters.
  $header_value = NULL;
  $header_parameters = [];

  // Split the provided value by ';' (semicolon), which we assume is the
  // character is used to separate the parameters.
  $parameter_pairs = explode(';', $value);

  // Iterate through the extracted parameters, and prepare each of them to be
  // added to a parameterized message header. There should be a single text
  // parameter and one or more key/value parameters in the provided header
  // value. We assume that a '=' (equals) character is used to separate the
  // key and value for each of the parameters.
  foreach ($parameter_pairs as $parameter_pair) {

    // Find out whether the current parameter pair really is a parameter
    // pair or just a single value.
    if (preg_match('/=/', $parameter_pair) > 0) {

      // Split the parameter so that we can access the parameter's key and
      // value separately.
      $parameter_pair = explode('=', $parameter_pair);

      // Validate that the parameter has been split in two, and that both
      // the parameter's key and value is accessible. If that is the case,
      // then add the current parameter's key and value to the array which
      // holds all parameters to be added to the current header.
      if (!empty($parameter_pair[0]) && !empty($parameter_pair[1])) {
        $header_parameters[trim($parameter_pair[0])] = trim($parameter_pair[1]);
      }
    }
    else {
      $header_value = trim($parameter_pair);
    }
  }

  // Add the parameterized header.
  $message
    ->getHeaders()
    ->addParameterizedHeader($key, $header_value, $header_parameters);
}