Same name and namespace in other branches
  1. 4.7.x modules/contact.module \contact_mail_user_submit()
  2. 6.x modules/contact/contact.pages.inc \contact_mail_user_submit()

Process the personal contact page form submission.

File

modules/contact/contact.module, line 356
Enables the use of personal and site-wide contact forms.

Code

function contact_mail_user_submit($form_id, $form_values) {
  global $user;
  $account = user_load(array(
    'uid' => arg(1),
    'status' => 1,
  ));

  // Compose the body:
  $message[] = "{$account->name},";
  $message[] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array(
    '!name' => $user->name,
    '!name-url' => url("user/{$user->uid}", NULL, NULL, TRUE),
    '!form-url' => url($_GET['q'], NULL, NULL, TRUE),
    '!site' => variable_get('site_name', 'Drupal'),
  ));
  $message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array(
    '!url' => url("user/{$account->uid}", NULL, NULL, TRUE),
  ));
  $message[] = t('Message:');
  $message[] = $form_values['message'];

  // Tidy up the body:
  foreach ($message as $key => $value) {
    $message[$key] = wordwrap($value);
  }

  // Prepare all fields:
  $to = $account->mail;
  $from = $user->mail;

  // Format the subject:
  $subject = '[' . variable_get('site_name', 'Drupal') . '] ' . $form_values['subject'];

  // Prepare the body:
  $body = implode("\n\n", $message);

  // Send the e-mail:
  drupal_mail('contact-user-mail', $to, $subject, $body, $from);

  // Send a copy if requested:
  if ($form_values['copy']) {
    drupal_mail('contact-user-copy', $from, $subject, $body, $from);
  }

  // Log the operation:
  flood_register_event('contact');
  watchdog('mail', t('%name-from sent %name-to an e-mail.', array(
    '%name-from' => $user->name,
    '%name-to' => $account->name,
  )));

  // Set a status message:
  drupal_set_message(t('The message has been sent.'));

  // Jump to the user's profile page:
  return "user/{$account->uid}";
}