You are here

function smtp_prefered_provider in SMTP Authentication Support 7.2

Gets the provider for the first met selection criteria.

Parameters

message: The message being sent. Its values will be used to check selection criterias.

Return value

A string with the machine name of the selected provider. If none of the selection criterias are satisfied, this function returns NULL.

1 call to smtp_prefered_provider()
SmtpMailSystem::mailWithoutQueue in ./smtp.mail.inc

File

./smtp.module, line 274
Enables Drupal to send e-mail directly to an SMTP server.

Code

function smtp_prefered_provider($message) {
  static $drupal_static;
  if (!isset($drupal_static)) {
    $drupal_static =& drupal_static(__FUNCTION__);
  }
  $prefered_provider = NULL;

  // Gets prefered provider from cache
  if (isset($drupal_static['provider'])) {
    $prefered_provider =& $drupal_static['provider'];

    // Checks if conditions are still valid
    foreach ($drupal_static['criteria'] as $key => $value) {
      if (empty($value) || strpos($key, 'message_') !== 0 || $key == 'message_language') {
        continue;
      }
      if (!isset($message[$key]) || $message[$key] != $value) {

        // Conditions changed
        $prefered_provider = NULL;
        break;
      }
    }

    // Language is an object and checked separately
    if (!empty($drupal_static['criteria']['message_language'])) {
      $value = $drupal_static['criteria']['message_language'];
      if (!isset($message['language']) || $message['language']->language != $value) {

        // Conditions changed
        $prefered_provider = NULL;
      }
    }
  }

  // Gets provider from database
  if (empty($prefered_provider)) {
    $query = db_select('smtp_selection_criteria', 'c')
      ->fields('c')
      ->orderBy('weight')
      ->range(0, 1);

    // Adds conditions for the listed keys
    $message_keys = array(
      'module',
      'key',
    );
    foreach ($message_keys as $key) {
      $condition = db_or()
        ->condition("message_{$key}", '');
      if (isset($message[$key])) {
        $condition
          ->condition("message_{$key}", $message[$key]);
      }
      $query
        ->condition($condition);
    }

    // Language is an object and added separately
    $condition = db_or()
      ->condition('message_language', '');
    if (isset($message['language'])) {
      $condition
        ->condition('message_language', $message['language']->language);
    }
    $query
      ->condition($condition);

    // Caches selected provider
    $result = $query
      ->execute()
      ->fetchAssoc();
    if ($result) {
      $prefered_provider = $result['provider'];
      $drupal_static['criteria'] = $result;
    }
  }
  return $prefered_provider;
}