I've created a contact form using PHP mail() directly inside a page. It successfully emails te contact information to the recipient. However, it won't advance to the confirmation page identified in header() and outputs the following error:

Warning: Cannot modify header information - headers already sent by (output started at /home/redacted/redacted.com/wp-content/themes/diner/includes/theme-functions.php:752) in /home/redacted/redacted.com/wp-content/themes/diner/page.php on line 163

Any advice or resource links on how to fix this?

function spamcheck($field)
  //filter_var() sanitizes the e-mail
  //address using FILTER_SANITIZE_EMAIL
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() validates the e-mail
  //address using FILTER_VALIDATE_EMAIL
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    return TRUE;
    return FALSE;

if (isset($_REQUEST['contact_email']))
  {//if "email" is filled out, proceed

  //check if the email address is invalid
  $mailcheck = spamcheck($_REQUEST['contact_email']);
  if ($mailcheck==FALSE)
    echo "Invalid input";
    {//send email
    $inquirer_name = $_REQUEST['contact_name'] ;
    $email = $_REQUEST['contact_email'] ;
"Subject: Test contact",
"Name: $inquirer_name
Email: $email "
  } else { //if "email" is not filled out, display the form ?>

<form method="post" action="http://redacted.com/contact">
<li>First Name: <input name="contact_name" type="text" /></li>
<li>Email: <input name="contact_email" type="text" /></li>
<li><input type='submit' /></li>

<?php } ?>
  • What happens when you are using wp_mail(). There is no excuse to use plain mail(). ;) – fuxia Nov 20 '12 at 2:06

You can't use header in the middle of a page, content has already been sent to the browser, so that means headers were already sent.

Look at the code in the myriad of form plugins out there to see how they handle this- hook an action before the template is loaded and do your form processing and checking there.

| improve this answer | |
  • Ah, I see: hence that specific error. I will look into it, thanks! – Marc P Nov 20 '12 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.