You are here

function email_mail_page_form_submit in Email Field 6.2

Same name and namespace in other branches
  1. 5 email.module \email_mail_page_form_submit()
  2. 6 email.module \email_mail_page_form_submit()
  3. 7 email.module \email_mail_page_form_submit()

Process the site-wide contact page form submission.

File

./email.module, line 298

Code

function email_mail_page_form_submit($form_id, $edit) {
  $nid = intval(arg(1));
  $fieldname = arg(2);
  if (empty($nid) || empty($fieldname)) {
    drupal_not_found();
    return;
  }
  $node = node_load($nid);
  if (!$node) {
    drupal_not_found();
    return;
  }

  // Validate field name
  $types = content_types($node->type);
  if (!isset($types['fields'][$fieldname]) || $types['fields'][$fieldname]['type'] != 'email') {
    drupal_not_found();
    return;
  }
  $field = $node->{$fieldname};
  if (empty($field) || empty($field[0]['email'])) {
    drupal_not_found();
    return;
  }
  $email = $field[0]['email'];

  // E-mail address of the sender: as the form field is a text field,
  // all instances of \r and \n have been automatically stripped from it.
  $from = $edit['mail'];

  // Compose the body:
  $message[] = t("@name sent a message using the contact form at @form.", array(
    '@name' => $edit['name'],
    '@form' => url($_GET['q'], NULL, NULL, TRUE),
  ));
  $message[] = $edit['message'];

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

  // Format the category:
  $subject = t('[@title - @contact] @subject', array(
    '@title' => preg_replace("/\r|\n/", '', $node->title),
    '@contact' => $types['fields'][$fieldname]['widget']['label'],
    '@subject' => $edit['subject'],
  ));

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

  // Send the e-mail to the recipients:
  drupal_mail($fieldname, $email, $subject, $body, $from);

  // Log the operation:
  flood_register_event('email');
  watchdog('mail', t('%name-from sent an e-mail at %form.', array(
    '%name-from' => theme('placeholder', $edit['name'] . " <{$from}>"),
    '%form' => url($_GET['q'], NULL, NULL, TRUE),
  )));

  // Update user:
  drupal_set_message(t('Your message has been sent.'));

  // Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
  return 'node/' . $node->nid;
}