You are here

function redhen_fields_email_action in RedHen CRM 7

Sends an e-mail message.

Parameters

object $entity: An optional node object, which will be added as $context['node'] if provided.

array $context: Array with the following elements:

  • 'subject': The subject of the message. This will be passed through token_replace().
  • 'message': The message to send. This will be passed through token_replace().
  • Other elements will be used as the data for token replacement.

File

modules/redhen_fields/redhen_fields.module, line 839
Defines email, phone and address field types for RedHen CRM.

Code

function redhen_fields_email_action($entity, $context) {
  if (empty($context['node'])) {
    $context['node'] = $entity;
  }
  $recipient = FALSE;
  switch ($entity
    ->entityType()) {
    case 'redhen_org':

      // If we have a primary contact, send email to them.
      if (isset($entity->primary_contact)) {
        $recipient = $entity->primary_contact
          ->email();
      }
      break;
    case 'redhen_contact':
      $recipient = $entity
        ->email();
      break;
  }
  if ($recipient) {

    // If the recipient is a registered user with a language preference, use
    // the recipient's preferred language. Otherwise, use the system default
    // language.
    $recipient_account = user_load_by_mail($recipient);
    if ($recipient_account) {
      $language = user_preferred_language($recipient_account);
    }
    else {
      $language = language_default();
    }
    $params = array(
      'context' => $context,
    );
    if (drupal_mail('system', 'redhen_email_action', $recipient, $language, $params)) {
      watchdog('action', 'Sent email to %recipient', array(
        '%recipient' => $recipient,
      ));
    }
    else {
      watchdog('error', 'Unable to send email to %recipient', array(
        '%recipient' => $recipient,
      ));
    }
  }
}