You are here

function crm_core_contact_send_email_action in CRM Core 7

Send e-mail to contacts action.

File

modules/crm_core_contact/crm_core_contact.module, line 1589
Provides default CRM Core Contact entities and the ability to create more.

Code

function crm_core_contact_send_email_action($contact, $context) {
  if (module_exists('token')) {

    // Token replacement preparations.
    $data = array(
      token_get_entity_mapping('entity', 'crm_core_contact') => $contact,
    );
    $options = array(
      // Remove tokens that could not be found.
      'clear' => TRUE,
    );
    $subject = token_replace($context['subject'], $data, $options);
    $message = token_replace($context['message'], $data, $options);
  }
  else {
    $subject = $context['subject'];
    $message = $context['message'];
  }
  $contact_wrapper = entity_metadata_wrapper('crm_core_contact', $contact);
  $email = $contact_wrapper->primary_email
    ->value();
  if (!empty($email)) {

    // Getting sender email.
    $account = $GLOBALS['user'];
    $from = empty($account->mail) ? NULL : $account->mail;
    if (module_exists('crm_core_user_sync')) {
      $sender = crm_core_user_sync_get_contact_from_uid($account->uid);
      if ($sender) {
        $sender_wrapper = entity_metadata_wrapper('crm_core_contact', $sender);
        $sender_mail = $sender_wrapper->primary_email
          ->value();
        if (!empty($sender_mail)) {
          $from = $sender_mail;
        }
      }
    }
    $params = array(
      'subject' => $subject,
      'message' => $message,
      'crm_core_contact' => $contact_wrapper,
    );
    drupal_mail('crm_core_contact', 'crm-core-contact-send-email-action', $email, language_default(), $params, $from);
  }
}