You are here

function message_notify_send_mail in Message Notify 7

Same name and namespace in other branches
  1. 7.2 message_notify.module \message_notify_send_mail()

Process and send a message.

2 calls to message_notify_send_mail()
message_notify_example_comment_insert in message_notify_example/message_notify_example.module
Implements hook_comment_insert().
message_notify_rules_process in ./message_notify.rules.inc
Action: Process and send Message.

File

./message_notify.module, line 11
Message notify.

Code

function message_notify_send_mail(Message $message, $options = array()) {

  // Set default values.
  $options += array(
    'save on fail' => TRUE,
    'save on success' => TRUE,
    'rendered subject field' => FALSE,
    'rendered body field' => FALSE,
    // Override email address.
    'mail' => FALSE,
    // Override user's language, and use Message language.
    'language override' => FALSE,
  );
  $account = user_load($message->uid);
  $element = $message
    ->buildContent('full', $account->language);
  $message->rendered_subject = $element['subject']['#markup'];
  $message->rendered_body = $element['text']['#markup'];
  $wrapper = entity_metadata_wrapper('message', $message);
  if ($options['rendered subject field']) {
    $wrapper->{$options['rendered subject field']}
      ->set($message->rendered_subject);
  }
  if ($options['rendered body field']) {

    // Get the format from the field. We assume the first delta is the
    // same as the rest.
    if (empty($wrapper->{$options['rendered body field']}->format)) {
      $wrapper->{$options['rendered body field']}
        ->set($message->rendered_body);
    }
    else {
      $format = $wrapper->type->{MESSAGE_FIELD_MESSAGE_TEXT}
        ->get(0)->format
        ->value();
      $wrapper->{$options['rendered body field']}
        ->set(array(
        'value' => $message->rendered_body,
        'format' => $format,
      ));
    }
  }
  $params = array(
    'message_entity' => $message,
    'rendered_subject' => $message->rendered_subject,
    'rendered_body' => $message->rendered_body,
  );
  $mail = $options['mail'] ? $options['mail'] : $account->mail;
  $languages = language_list();
  if (!$options['language override']) {
    $lang = !empty($account->language) && $account->language != LANGUAGE_NONE ? $languages[$account->language] : language_default();
  }
  else {
    $lang = $languages[$message->language];
  }
  $result = drupal_mail('message_notify', $message->type, $mail, $lang, $params);
  if (!$result) {
    watchdog('message_notify', t('Could not send "@type" email to user ID @uid'), array(
      '@message' => $message->type,
      '@uid' => $message->uid,
    ), WATCHDOG_ERROR);
    if ($options['save on fail']) {
      $message
        ->save();
    }
  }
  elseif ($result && $options['save on success']) {
    $message
      ->save();
  }
  return $result;
}