-1

I got an "Warning: Cannot modify header information - headers already sent (output started at process_contact.php:5) in (/process_contact.php on line 147 )" while using mail() in php for sending an mail to multiple user.

    $from = "test@gmail.com";
    $adminmessage="You have received an email from ".$_POST['email'].
    $to = "test1@gmail.com";
    $subject = "Email Received for an Order";
    $message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body>
    Online Reservation Form has been submitted on the website on '.$date.'.<br><br>
<table width="710px" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="40%" height="30" valign="top">Name:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["name"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Address:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["address"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Phone no.:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["phone"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">E-mail:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["email"].'</b></td>
  </tr>
  <tr>    
     <tr>
    <td width="40%" height="30" valign="top">Check Out:</td>
    <td width="60%" height="30" valign="top"><b>'. $_POST["price"].'</b></td>
  </tr>

  <tr>
    <td width="40%" height="30" valign="top">Night:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["night"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Rooms:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["rooms"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Adults:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["adults"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Children:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["children"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Room Type:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["roomtype"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Price:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["price"].'</b></td>
  </tr>
  <tr>
    <p><strong>Payment Details</strong></p>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Payment Type:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["paymentype"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Card Name Holder:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["cardholder"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Credit Card no.:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["creditcardno"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Expiration Date:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["exp"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Security Code:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["securitycode"].'</b></td>
  </tr>
  <tr>
    <p>Payment Details</strong></p>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Billing Address( Street adress ):</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["billing1"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Billing Address( City/State/Zip ):</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["billing2"].'</b></td>
  </tr>
  <tr>
    <td width="40%" height="30" valign="top">Special Request:</td>
    <td width="60%" height="30" valign="top"><b>'.$_POST["comments"].'</b></td>
  </tr>

</table>
    </body></html>';

    $headers = "From:" . $from."\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
    ini_set("SMTP","192.168.0.30");
    $date = date("m/d/Y H:i:s");
    mail($to,$subject,$message,$headers);
    //for client
    $from="test@gmail.com";
    $to1=$_POST['email'];
    $subject1="Confirmation";
    $clentmessage1 ='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body><p>Dear Customer! &nbsp;'.$_POST["name"].'.Your reservation  has been confirmed.<br>
Thank you for Emailing us. We will get back to you shortly.<br/>
Plz donot hesitate to contact us atfor further information or any queries!<br/><br/><br/>
<strong>Super Value Inn</strong>
</body>
</html>';
    $headers = "From:" . $from."\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
    ini_set("SMTP","192.168.0.30");
    mail($to1,$subject1,$clentmessage1,$headers);
    echo "Mail Sent.";
    header('Location: reservation.php?sentok');
    exit();
}
else
{
    header('Location: reservation.php?err');
}
Yogs
  • 78
  • 7
  • `$adminmessage="You have received an email from ".$_POST['email'].` Note the `.` at the end. I think you meant a `;` – tigrang May 10 '12 at 07:59
  • this question is asked almost daily, please search first. –  May 10 '12 at 08:35

3 Answers3

5

You cannot send header information after you have already echoed something. See PHP output buffering for information on how to get around this.

Drop this and it should work:

echo "Mail Sent.";
2
echo "Mail Sent.";
header('Location: reservation.php?sentok');

That ^

In any case, it doesn't make sense anyway to echo something then directly redirect. You could instead output Mail Sent at reservation.php

if(isset($_GET['sentok'])) echo 'Mail Sent.';

EDIT:

I think you got a syntax error there as pointed by your error message:

$adminmessage="You have received an email from ".$_POST['email']. // <-- this
$to = "test1@gmail.com";

This gives me an Undefined Variable notice in my local setup, which in turns stopping you from calling header() as that notice is counted as an output by php.

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
1

You have an echo statement before the header() call, that will cause headers to be sent. You must not output anything before a header()

gunnx
  • 1,229
  • 7
  • 16
  • Thanks everyone for your effort.I got solved my problem.Problem is actually caused by ini_set SMtp – Yogs May 11 '12 at 01:51