You are here

function messaging_admin_test_post_form_submit in Messaging 7

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

Post test message

File

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

Code

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

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

  // Destination may be account or plain parameter/s
  $address = $form_state['values']['to'];
  $send_method = messaging_send_method($method);
  if ($account = user_load_by_name($address)) {
    $vars['!name'] = theme('username', array(
      'account' => $account,
    ));
    $message
      ->set_user($account);
    if ($dest = $message
      ->get_destinations()) {
      $destination = reset($dest);
      drupal_set_message(t('Found address @address for user !name', array(
        '@address' => $destination
          ->format('long'),
      ) + $vars));
      $result = $message
        ->send();
    }
    else {
      drupal_set_message(t('Cannot find a valid address for user !name', $vars), 'error');
    }
  }
  elseif ($destination = $send_method
    ->address_destination($address, TRUE)) {
    drupal_set_message(t('Sending message to address: @address', array(
      '@address' => $address,
    )));
    $result = $message
      ->add_destination($destination)
      ->send();
  }
  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');
  }
}