You are here

function contact_attach_contact_site_form_submit in Contact Attach 7

Form submission handler for contact_site_form().

Overrides contact_site_form_submit().

See also

contact_attach_contact_form_validate()

contact_attach_form_alter()

File

./contact_attach.module, line 191
Allows attaching files to messages sent using contact forms.

Code

function contact_attach_contact_site_form_submit($form, &$form_state) {
  global $user, $language;
  $values = $form_state['values'];
  $values['sender'] = $user;
  $values['sender']->name = $values['name'];
  $values['sender']->mail = $values['mail'];
  $values['category'] = contact_load($values['cid']);

  // Save the anonymous user information to a cookie for reuse.
  if (!$user->uid) {
    user_cookie_save(array_intersect_key($values, array_flip(array(
      'name',
      'mail',
    ))));
  }

  // Get the to and from e-mail addresses.
  $to = $values['category']['recipients'];
  $from = $values['sender']->mail;

  // Send the e-mail to the recipients using the site default language.
  $results['mail'] = drupal_mail('contact', 'page_mail', $to, language_default(), $values, $from);

  // If the user requests it, send a copy using the current language.
  if ($values['copy']) {
    $results['copy'] = drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
  }

  // Send an auto-reply if necessary using the current language.
  if ($values['category']['reply']) {
    $results['autoreply'] = drupal_mail('contact', 'page_autoreply', $from, $language, $values, $to);
  }
  if (!empty($results['mail']['result'])) {
    flood_register_event('contact', variable_get('contact_threshold_window', 3600));
    watchdog('mail', '%sender-name (@sender-from) sent an e-mail regarding %category.', array(
      '%sender-name' => $values['name'],
      '@sender-from' => $from,
      '%category' => $values['category']['category'],
    ));
    $user_message = t('Your message has been sent.');
    if ($values['copy'] && empty($results['copy']['result'])) {
      watchdog('mail', 'The mail system failed to send a copy of the message to the site-wide contact form user.', array(), WATCHDOG_ERROR);
      $user_message .= ' ' . t('However, the copy asked for failed to send.');
    }
    if ($values['category']['reply'] && empty($results['autoreply']['result'])) {
      watchdog('mail', 'The mail system failed to send an auto-reply to the site-wide contact form user.', array(), WATCHDOG_ERROR);
    }
    drupal_set_message($user_message);
  }
  else {
    watchdog('mail', '%sender-name (@sender-from) attempted to send an e-mail regarding %category, but was unsuccessful.', array(
      '%sender-name' => $values['name'],
      '@sender-from' => $from,
      '%category' => $values['category']['category'],
    ), WATCHDOG_ERROR);
    drupal_set_message(t('There was a problem sending your message. Please try again later.'), 'error');
  }

  // Jump to home page rather than back to contact page to avoid contradictory
  // messages if flood control has been activated.
  $form_state['redirect'] = '';
}