You are here

function _mailsystem_delegate_get_mailsystem in Mail System 7.3

Returns an MailSystemInterface class instance for a given action.

Of a class implementing the MailSystemInterface responsible for performing the given action.

Parameters

string $module: The machine-readable module name.

string $key: The email key.

string $action: The name of the MailSystemInterface method to be invoked. Should be one of:

  • format: Format a message composed by drupal_mail() prior to sending.
  • mail: Send a message composed by drupal_mail().

Return value

MailSystemInterface A mail system implementation.

See also

drupal_mail_system()

2 calls to _mailsystem_delegate_get_mailsystem()
MailsystemDelegateMailSystem::format in ./MailsystemDelegateMailSystem.inc
Implements MailSystemInterface::format().
MailsystemDelegateMailSystem::mail in ./MailsystemDelegateMailSystem.inc
Implements MailSystemInterface::mail().

File

./mailsystem.module, line 201
Provide UI for controlling the mail_system variable.

Code

function _mailsystem_delegate_get_mailsystem($module, $key, $action) {
  $instances =& drupal_static(__FUNCTION__, array());

  // Try to use custom settings defined for the message's module and key.
  $settings = variable_get('mailsystem_delegate:' . $module . '_' . $key);
  if ($settings === NULL) {

    // Try to use custom settings defined for the message's module.
    $settings = variable_get('mailsystem_delegate:' . $module);
  }
  if ($settings === NULL) {

    // Try to use the configured site-wide default mail system settings.
    $settings = variable_get('mailsystem_delegate:' . mailsystem_default_id());
  }

  // Use the class configured for the given action.
  if ($settings && isset($settings[$action])) {
    $class = $settings[$action];
  }
  else {
    $mailsystems = mailsystem_get();
    $class = $mailsystems[mailsystem_default_id()];

    // Fallback to drupal default in order to prevent an infinite recursion.
    if ($class == 'MailsystemDelegateMailSystem') {
      $class = mailsystem_default_value();
    }
  }
  if (empty($instances[$class])) {
    $interfaces = class_implements($class);
    if (isset($interfaces['MailSystemInterface'])) {
      $instances[$class] = new $class();
    }
    else {
      throw new Exception(t('Class %class does not implement interface %interface', array(
        '%class' => $class,
        '%interface' => 'MailSystemInterface',
      )));
    }
  }
  return $instances[$class];
}