You are here

function views_send_mail_action in Views Send 6

Main action callback.

See also

http://drupal.org/node/172152

1 string reference to 'views_send_mail_action'
views_send_form_alter in ./views_send.module
Implementation of hook_form_alter().

File

./views_send.module, line 342
The Views Send module.

Code

function views_send_mail_action($object, $context) {
  global $user;

  // From: parts.
  $from_mail = trim($context['views_send_from_mail']);
  $from_name = $context['views_send_from_name'];

  // To: parts.
  $to_mail = trim(_views_send_get_from_views_result($context['row'], $context['views_send_to_mail'], 'email'));
  $to_name = _views_send_get_from_views_result($context['row'], $context['views_send_to_name']);

  // Formatting using selected input format.
  $subject = $context['views_send_subject'];
  $body = $context['views_send_message_format'] == VIEWS_SEND_FORMAT_PLAIN ? $context['views_send_message'] : check_markup($context['views_send_message'], $context['views_send_message_format']);

  // Populate row/context tokens.
  $token_keys = $token_values = array();
  foreach ($context['views_send_tokens'] as $field_key => $field_name) {
    $token_keys[] = VIEWS_SEND_TOKEN_PREFIX . sprintf(VIEWS_SEND_TOKEN_PATTERN, $field_name) . VIEWS_SEND_TOKEN_POSTFIX;
    $token_values[] = _views_send_get_from_views_result($context['row'], $field_key);
  }
  $subject = str_replace($token_keys, $token_values, $subject);
  $body = str_replace($token_keys, $token_values, $body);

  // Let Token module operate substitutions.
  if (module_exists('token')) {
    _views_send_normalize_context($context);
    $subject = token_replace_multiple($subject, $context);
    $body = token_replace_multiple($body, $context);
  }

  // Process PHP code when only plain format is available.
  if (!VIEWS_SEND_MIMEMAIL && _views_send_allow_php() && $context['views_send_message_format'] == VIEWS_SEND_FORMAT_PLAIN) {
    $body = drupal_eval($body);
  }

  // We transform receipt, priority in headers,
  // merging them to the user defined headers.
  $headers = _views_send_headers($context['views_send_receipt'], $context['views_send_priority'], $from_mail, $context['views_send_headers']);
  $attachments = $context['views_send_attachments'] ? $context['views_send_attachments'] : array();
  $format = $context['views_send_message_format'];

  // All tokens replacements, PHP processing and formatting were done.
  // We are performing now all usual mail processing, altering and preparing.
  _views_send_prepare_mail($from_name, $from_mail, $to_name, $to_mail, $subject, $body, $headers, $format, $attachments);

  // Queue the message to the spool table.
  db_query("INSERT INTO {views_send_spool} (uid, timestamp, from_name, from_mail, to_name, to_mail, subject, body, headers) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $user->uid, time(), $from_name, $from_mail, $to_name, $to_mail, $subject, $body, serialize($headers));
}