You are here

function system_send_email_action in Drupal 6

Same name and namespace in other branches
  1. 7 modules/system/system.module \system_send_email_action()

Implementation of a configurable Drupal action. Sends an email.

File

modules/system/system.module, line 1732
Configuration system that lets administrators modify the workings of the site.

Code

function system_send_email_action($object, $context) {
  global $user;
  switch ($context['hook']) {
    case 'nodeapi':

      // Because this is not an action of type 'node' the node
      // will not be passed as $object, but it will still be available
      // in $context.
      $node = $context['node'];
      break;

    // The comment hook provides nid, in $context.
    case 'comment':
      $comment = $context['comment'];
      $node = node_load($comment->nid);
      break;
    case 'user':

      // Because this is not an action of type 'user' the user
      // object is not passed as $object, but it will still be available
      // in $context.
      $account = $context['account'];
      if (isset($context['node'])) {
        $node = $context['node'];
      }
      elseif ($context['recipient'] == '%author') {

        // If we don't have a node, we don't have a node author.
        watchdog('error', 'Cannot use %author token in this context.');
        return;
      }
      break;
    default:

      // We are being called directly.
      $node = $object;
  }
  $recipient = $context['recipient'];
  if (isset($node)) {
    if (!isset($account)) {
      $account = user_load(array(
        'uid' => $node->uid,
      ));
    }
    if ($recipient == '%author') {
      $recipient = $account->mail;
    }
  }
  if (!isset($account)) {
    $account = $user;
  }
  $language = user_preferred_language($account);
  $params = array(
    'account' => $account,
    'object' => $object,
    'context' => $context,
  );
  if (isset($node)) {
    $params['node'] = $node;
  }
  if (drupal_mail('system', 'action_send_email', $recipient, $language, $params)) {
    watchdog('action', 'Sent email to %recipient', array(
      '%recipient' => $recipient,
    ));
  }
  else {
    watchdog('error', 'Unable to send email to %recipient', array(
      '%recipient' => $recipient,
    ));
  }
}