You are here

function mimemail_send_mail_action in Mime Mail 6

Implements a configurable Drupal action. Sends an email.

File

modules/mimemail_action/mimemail_action.module, line 30
Provide actions for the Mime Mail module.

Code

function mimemail_send_mail_action(&$object, $context = array()) {
  global $user;
  $key = !empty($context['key']) ? $context['key'] : 'action_send_mail';
  switch ($context['hook']) {
    case 'nodeapi':

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

      // The comment hook provides nid, in $context.
      $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 (strpos($context['recipient'], '%author') !== FALSE) {

        // 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;
  }
  $to = $context['to'];
  if (isset($node)) {
    if (!isset($account)) {
      $account = user_load(array(
        'uid' => $node->uid,
      ));
    }
    if ($to == '%author') {
      $to = $account->mail;
    }
  }
  if (!isset($account)) {
    $account = $user;
  }
  $params = array_merge(array(
    'account' => $account,
    'object' => $object,
  ), $context);
  if (isset($node)) {
    $params['node'] = $node;
  }
  $message = drupal_mail('mimemail', $key, $to, user_preferred_language($account), $params, NULL, FALSE);
  $message = mimemail($message['from'], $message['to'], $message['subject'], $message['body'], NULL, $message['headers'], $message['params']['plaintext'], $message['params']['attachments'], $message['id']);
  $recipients = trim(implode(', ', array_merge(explode(',', $recipient), explode(',', $context['bcc']), explode(',', $context['cc']))), ', ');
  if ($message['result']) {
    watchdog('action', 'Sent HTML email to %recipients', array(
      '%recipients' => $recipients,
    ));
  }
  else {
    watchdog('error', 'Unable to send HTML email to %recipients', array(
      '%recipient' => $recipients,
    ));
  }
}