You are here

function messaging_admin_test_post_form_submit in Messaging 6.4

Same name and namespace in other branches
  1. 7 messaging.admin.inc \messaging_admin_test_post_form_submit()

Post test message

File

./messaging.admin.inc, line 147
Messaging Framework - Admin UI

Code

function messaging_admin_test_post_form_submit($form, $form_state) {
  global $user;
  messaging_log_start();

  // Convert body in array of lines
  $body = split("\n", $form_state['values']['body']);
  $body = array_map('trim', $body);
  $method = $form_state['values']['method'];
  $message = messaging_message_build(array(
    'type' => 'test',
    'subject' => $form_state['values']['subject'],
    'body' => $body,
    'method' => $method,
    'priority' => 1,
  ));

  // Destination may be account or plain parameter/s
  $destination = $form_state['values']['to'];
  $send_method = messaging_send_method($method);
  if ($account = user_load(array(
    'name' => $destination,
  ))) {
    $vars['!name'] = theme('username', $account);
    if ($address = $send_method
      ->get_user_address($account)) {
      drupal_set_message(t('Found address @address for user !name', array(
        '@address' => $address,
      ) + $vars));
      $result = messaging_message_send_user($account, $message, $method);
    }
    else {
      drupal_set_message(t('Cannot find a valid address for user !name', $vars), 'error');
    }
  }
  elseif ($send_method
    ->address_validate($destination)) {
    drupal_set_message(t('Sending message to address: @address', array(
      '@address' => $destination,
    )));
    $result = messaging_message_send(array(
      $destination,
    ), $message, $method);
  }
  else {
    drupal_set_message(t('The destination is not a user name nor a valid address.'), 'error');
  }
  if (isset($result)) {
    if ($result) {
      drupal_set_message(t('The message was sent successfully.'));
    }
    else {
      drupal_set_message(t('The message sending failed.'), 'warning');
    }
  }

  // Check some conditions and let the user know
  if (!$send_method->enabled) {
    drupal_set_message(t('This sending method is disabled, thus regular messages will be discarded.'), 'warning');
  }
}