You are here

function contact_attach_contact_personal_form_submit in Contact Attach 7

Form submission handler for contact_personal_form().

Overrides contact_personal_form_submit().

See also

contact_attach_contact_form_validate()

contact_attach_form_alter()

File

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

Code

function contact_attach_contact_personal_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'];

  // 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['recipient']->mail;
  $from = $values['sender']->mail;

  // Send the e-mail in the requested user language.
  $results['mail'] = drupal_mail('contact', 'user_mail', $to, user_preferred_language($values['recipient']), $values, $from);

  // Send a copy if requested, using current page language.
  if ($values['copy']) {
    $results['copy'] = drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
  }
  if (!empty($results['mail']['result'])) {
    flood_register_event('contact', variable_get('contact_threshold_window', 3600));
    watchdog('mail', '%sender-name (@sender-from) sent %recipient-name an e-mail.', array(
      '%sender-name' => $values['name'],
      '@sender-from' => $from,
      '%recipient-name' => $values['recipient']->name,
    ));
    $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 personal contact form user.', array(), WATCHDOG_ERROR);
      $user_message .= ' ' . t('However, the copy asked for failed to send.');
    }
    drupal_set_message($user_message);
  }
  else {
    watchdog('mail', '%sender-name (@sender-from) attempted to send %recipient-name an e-mail, but was unsuccessful.', array(
      '%sender-name' => $values['name'],
      '@sender-from' => $from,
      '%recipient-name' => $values['recipient']->name,
    ), WATCHDOG_ERROR);
    drupal_set_message(t('There was a problem sending your message. Please try again later.'), 'error');
  }

  // Jump to the contacted user's profile page if the user is allowed.
  $form_state['redirect'] = user_access('access user profiles') ? 'user/' . $values['recipient']->uid : '';
}