couldnot modify header information [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
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! '.$_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');
}

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.";

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.

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

Related

email sending on php

May mail is working fine but unknown sender error is showing on gmail after sending the mail. My php code is as follows
$sendto="testconcepttc#gmail.com";
$subject = "Payment details of ".$dmail['name'];
$message = "<br><br>";
$message .= '<table border="0" cellspacing="5" cellpadding="4" width="600"style="border-collapse:collapse; font-family:Lucida Sans; border-color:#CCCCCC">
<tr style="background-color:#023564; color:#FFF;">
<td colspan="2"><h3 align="center">Registration Information From Midterm Delhi CSI 2017</h3></td>
</tr>
<tr>
<td width="54%"><p>Name</p></td>
<td width="46%"><p>'.$dmail['name'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Age</p></td>
<td width="46%"><p>'.$dmail['age'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Institution</p></td>
<td width="46%"><p>'.$ucomp.'</p></td>
</tr>
<tr>
<td width="54%"><p>Designation</p></td>
<td width="46%"><p>'.$dmail['designation'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Postal Address</p></td>
<td width="46%"><p>'.$dmail['address'].'</p></td>
</tr>
<tr>
<td width="54%"><p>City</p></td>
<td width="46%"><p>'.$dmail['city'].'</p></td>
</tr>
<tr>
<td width="54%"><p>State</p></td>
<td width="46%"><p>'.$dmail['state'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Pin</p></td>
<td width="46%"><p>'.$dmail['pin'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Country</p></td>
<td width="46%"><p>'.$dmail['country'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Phone (Off).</p></td>
<td width="46%"><p>'.$dmail['phone_off'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Phone (Res).</p></td>
<td width="46%"><p>'.$dmail['phone_res'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Mobile No</p></td>
<td width="46%"><p>'.$dmail['phone'].'</p></td>
</tr>
<tr>
<td width="54%"><p>E-mail</p></td>
<td width="46%"><p>'.$email.'</p></td>
</tr>
<tr>
<td width="54%"><p>Gender</p></td>
<td width="46%"><p>'.$dmail['gender'].'</p></td>
</tr>
<tr>
<td width="54%"><p>Food Preference</p></td>
<td width="46%"><p>'.$dmail['dite'].'</p></td>
</tr>
<tr>
<td width="54%"><p>CSI Number</p></td>
<td width="46%"><p>'.$dmail['ioa_membership_no'].'</p></td>
</tr>
<tr>
<td width="54%"><p>MCI Number</p></td>
<td width="46%"><p>'.$csi.'</p></td>
</tr>
</table>';
$username=$dmail['name'];
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$header .= "From: ".$username."\r\n";
$header .="\r\n";
$retval = mail ($sendto,$subject,$message,$header);
return $retval;
Try to add Reply-To header.
$header .= 'From: from#email.com' . "\r\n";
$header .= 'Reply-To: from#email.com' . "\r\n";
OR with name:
$header .= 'From: "From Name" <from#email.com>' . "\r\n";
$header .= 'Reply-To: "From Name" <from#email.com>' . "\r\n";

Outlook Receiving Mails in HTML Code

In my project i have done some email code to receive the order details, 1 mail to admin and other mail for user..
But some users and admins are receiving the emails with html code.. I checked in many ways with gmail, webmail.. All are displaying good.. Finally I tested with outlook and in outlook mail was getting in html code.. By enquiring the users they said that they r using outlook.. So that issue was with outlook I think so..
Can anyone help me to solve this issue.. Here is the source code which was used for displaying in email message.
$to = $obj_check_out->email;
$subject = "NEW YORK PRODUCT ORDER";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Instyle Customer Accounts <vamsi#mail.com>" . "\r\n";
$message = '<table width="100%" align="center">
<tbody><tr><td bgcolor="#393939">
<br>
<br>
<table cellspacing="0" cellpadding="0" width="650" border="0" align="center">
<tbody>
<tr>
<td width="10" bgcolor="#efefef">
<img src="images/newsletter/top_left.jpg" class="CToWUd">
</td>
<td width="630" height="92" bgcolor="#efefef" background="images/newsletter/top_bg.jpg">
<table width="630">
<tbody>
<tr>
<td width="514">
<font color="#333333" style="font-family:Tahoma;font-size:12px">
<br>
<b><a target="_blank" href="#"><span class="il">NEWYORK</span>.COM</a> ORDER CONFIRMATION</b> </font>
<font color="#333333" style="font-family:Tahoma;font-size:10px">[ DATE: '.$response["date_ordered"].' ]</font>
</td>
<td width="104" align="right">
<font color="#333333" style="font-family:Tahoma;font-size:12px">
<br>
<b>ORDER#:</b></font>
<font color="#333333" style="font-family:Tahoma;font-size:10px"> '.$response["order_log_id"].'</font>
</td>
</tr>
</tbody>
</table>
<br>
</td>
<td width="10" bgcolor="#efefef">
<img src="images/newsletter/top_right.jpg" class="CToWUd">
</td>
</tr>
<tr>
<td bgcolor="#efefef"> </td>
<td bgcolor="#efefef">
<font color="#333333">
<table cellspacing="0" cellpadding="2" width="630" border="0">
<tbody>
<tr>
<td height="35" bgcolor="#767676" background="images/newsletter/bar_bg.jpg" colspan="2">
<font color="#ffffff" style="font-family:Tahoma;font-size:12px">
<b>SHIPPING DETAILS</b></font>
</td>
</tr>
<tr>
<td width="170"> <font style="font-family:Tahoma;font-size:10px"><b>Name :</b></font></td>
<td width="452"><font style="font-family:Tahoma;font-size:10px">'.$response["firstname"].' '.$response["lastname"].'</font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>Address :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px">'.$response["ship_address1"].' '.$response["ship_address2"].'</font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>City :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px">'.$response["ship_city"].'</font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>State :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px">'.$response["ship_state"].'</font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>Country :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px">'.$response["ship_country"].'</font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>Zip :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px">'.$response["ship_zipcode"].'</font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>Phone :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px">'.$response["telephone"].'</font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>Email :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px"><a target="_blank" href="mailto:'.$response["email"].'">'.$response["email"].'</a></font></td>
</tr>
<tr>
<td> <font style="font-family:Tahoma;font-size:10px"><b>Courier :</b></font></td>
<td><font style="font-family:Tahoma;font-size:10px">'.$response["courier"].'</font></td>
</tr>
</tbody>
</table>
<br>
<table cellspacing="0" cellpadding="2" width="630" border="0">
<tbody><tr>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Thumb</b></font></td>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Item</b></font></td>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Style Number</b></font></td>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Size</b></font></td>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Color</b></font></td>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Quantity</b></font></td>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Price</b></font></td>
<td background="images/newsletter/bar_bg.jpg" align="center"><font color="#a1a1a1" style="font-family:Tahoma;font-size:11px"><b>Subtotal</b></font></td>
</tr>
'.$ordermsg.'
<tr>
<td align="right" colspan="7"><font style="font-family:Tahoma;font-size:12px">Grand-Total : </font></td>
<td align="right"><font style="font-family:Tahoma;font-size:12px">$'.$grandtotal.'</font></td>
</tr>
<tr>
<td align="right" colspan="7"><font style="font-family:Tahoma;font-size:9px">( For countries other than United State, you will be contacted by customer service for shipping fees ) </font></td>
<td align="center"></td>
</tr>
<tr>
<td align="center" colspan="8"><font style="color:red;font-family:Tahoma;font-size:9px"><br><br>* NOTE: Your order was received and will ship according to the availability notice on product page. </font><br></td>
</tr>
</tbody></table>
<table width="630" align="center" style="border-top:1px solid black">
<tbody><tr>
<td width="630" align="center">
<font color="#333333" style="font-family:Tahoma;font-size:10px">
<span class="il">Instyle</span> <span class="il">New</span> <span class="il">York</span>
230 West 38th Street
<span class="il">New</span> <span class="il">York</span>, NY 10018
PHONE: 212-840-0846 ext 22 EMAIL <a target="_blank" href="mailto:vamsi#gmail.com">info#<span class="il">company</span>.com</a>
</font>
</td>
</tr>
<tr>
<td width="630" align="center">
<font color="#333333" style="font-family:Tahoma;font-size:10px">
Purchaser agrees to abide by the <a target="_blank" href="#"><span class="il">company</span>.com</a> return policy.
</font>
</td>
</tr>
</tbody></table>
</font>
</td>
<td bgcolor="#efefef"> </td>
</tr>
<tr>
<td><img src="images/newsletter/bottom_left.jpg" class="CToWUd"></td>
<td><img src="images/newsletter/bottom_bg.jpg" class="CToWUd"></td>
<td><img src="images/newsletter/bottom_right.jpg" class="CToWUd"></td>
</tr>
</tbody>
</table>
<br><br>
</td></tr>
</tbody>
</table>';
mail($to,$subject,$message,$headers);
The email in outlook getting the same html code of $message with containing order details..
Thanks
If your code doesn't contain a valid <!doctype html> declaration and <body> tags etc, then that could contribute to the problem.
People using Outlook (or other similar mail clients) may have their settings set to not display HTML and images. I have seen that happen quite often before.
This stands at being a local issue and you have no control over that.
Therefore, you need to include a seperate header as TEXT only which is the usual norm when sending mail.
Use Phpmailer or Swiftmailer. That should solve everything.
References:
https://github.com/PHPMailer/PHPMailer
http://phpmailer.worxware.com/
http://swiftmailer.org/
and read the documentation on its implementation.
Other options are to use services such as MailChimp, Constant Contact etc. which work well and are services that are used widely and are already setup to handle both HTML and plain text formats.
http://mailchimp.com/
http://www.constantcontact.com/

What is wrong with my PHP email code; it won't redirect to thank you page [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
Curious as to what is wrong with the following php code which sends an email when a contact form is filled out and submitted. The email arrives perfectly but instead of redirecting it gives the following error:
Warning: Cannot modify header information - headers already sent by (output started at /home4/ranchoba/public_html/marvan/marvan/send.php:1) in /home4/ranchoba/public_html/marvan/marvan/send.php on line 112
I've tried calling the php file by the action in the form as well as by placing a in the very first line of the page. Neither one works.
PHP Code:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
//if the form is submitted, go ahead and send the email to ops
ob_start();
if ($_POST['is_form_submitted'] == "yes")
{
//Send email to ops of this request-----------------------------------------------------------------------------------
// multiple recipients
$to = 'blah#gmail.com';
// subject
$subject = "Contact Request: ".$_POST['name'];
// message
$message = '
<html>
<head>
<title>Re: Marvan Site Contact Request</title>
<style type="text/css">
<!--
.style4 {font-family: Arial, Helvetica, sans-serif; font-size: 12; }
.style5 {font-size: 12}
.style6 {font-family: Arial, Helvetica, sans-serif; font-size: 12; font-weight: bold; }
-->
</style>
</head>
<body>';
$message=$message.'
<table width="450" border="0" align="center" cellpadding="4" cellspacing="0" style="border-color:#CCCCCC; border-style:solid; border-width:1px; background-color:#FFFFFF; padding-top:3px;">
<tr>
<td width="430" style="height:auto;" valign="top">';
$message=$message.'<img src="http://www.blah.com/blah/images/logo.png">';
$message=$message.'<hr>';
$message=$message.'<h2 style="font-family:Arial, Helvetica, sans-serif; color:#B58736">Contact Request </h2>';
$message=$message.'
<table width="486" border="0" align="center" cellpadding="4" cellspacing="2">
<tr>
<td colspan="4" valign="top"><div align="left" class="style4"><span style="color:#000000; font-weight:bold;">Contact Details: </span></div></td>
</tr>
<tr>
<td width="145" valign="top"><div align="left" class="style4">Name :</div></td>
<td width="1" valign="top"><span class="style5"></span></td>
<td width="984" colspan="2" valign="top"><div align="left" class="style6">';
$message = $message. $_POST['name'];
$message = $message.'</div></td>
</tr>
<tr>
<td valign="top"><div align="left" class="style4">Email :</div></td>
<td valign="top"><span class="style5"></span></td>
<td colspan="2" valign="top"><div align="left" class="style6">';
$message = $message. $_POST['email'];
$message = $message.'</div></td>
</tr>
<tr>
<td valign="top"><div align="left" class="style4">Subject :</div></td>
<td valign="top"><span class="style5"></span></td>
<td colspan="2" valign="top"><div align="left" class="style6">';
$message = $message. $_POST['subject'];
$message = $message.'</div></td>
</tr>
<tr>
<td valign="top"><div align="left" class="style4">Phone :</div></td>
<td valign="top"><span class="style5"></span></td>
<td colspan="2" valign="top"><div align="left" class="style6">';
$message = $message. $_POST['phone'];
$message = $message.'</div></td>
</tr>
<tr>
<td valign="top"><div align="left" class="style4">Message :</div></td>
<td valign="top"><span class="style5"></span></td>
<td colspan="2" valign="top"><div align="left" class="style6">';
$message = $message. $_POST['message'];
$message = $message.'</div></td>
</tr>';
$message = $message.'
<tr> <tr><td colspan="4" valign="top"><span class="style5"></span></td>
</tr>';
$message = $message.'
</table>
<br /></td>
</tr>
</table>
<p> </p>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
/* $headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";*/
/* $headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";*/
// Mail it
mail($to, $subject, $message, $headers);
//end of Send Email to Client --------------------------------------------------------------
//redirect to the thank you page
header('Location: http://www.blah.com/blah/thank-you.php');
} //end of if ($_POST['is_form_submitted'] == "yes")
?>
Maybe you have space before <?php tag in your scripts. Check it please and remove them. Also use UTF-8 file encoding without BOM
It is best to find where you are outputting a space or line that is causing the header() to fail. But if you cannot, try this to clear the buffer before header().
if (ob_get_length() > 0 ) {
ob_clean();
flush();
}

Copy HTML form in email PHP

I am trying to copy my HTML form in email body. Below is code where the form is already in php file but I want if the user submits html form file then all the form data between that is not hidden is copied (as disabled elements) in the body of the email. Also if there is any file attachment field, it should be attached to the email.
<?php
// multiple recipients
$to = 'demo#localhost.com' . ', '; // note the comma
$to .= 'demo#localhost.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body style="padding:3px; margin:0px;" bgcolor="#FFFFFF">
<table cellpadding="0" cellspacing="0" border="0" width="440">
<tr><td style="height:10px"></td></tr>
<tr>
<td colspan="2" style="text-align:justify; line-height:15px;" class="body">
<form name="frm" method="POST" action="careersuccess.php" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td width="23%" class="body"> Name</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strname" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Address</td>
<td width="3%" class="body">:</td>
<td width="74%"><textarea cols="16" name="straddress"></textarea></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> City</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strcity" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> State</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strstate" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Contact No</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strno" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Email</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="stremail" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Comments</td>
<td width="3%" class="body">:</td>
<td width="74%"><textarea cols="16" name="strcomments"></textarea></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Resume</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="file" name="strresume"></td>
</tr>
<tr><td style="height:10px"></td></tr>
<tr>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I'll assume the form is submitted from a website, and that you're not trying to create a submittable form in an email. To copy the data submitted in the form, $_POST[""]; would surely be the best way to obtain the data. Obviously the data wouldn't be manipulatable once the email has arrived in the recipient's inbox but, as I say, I'm assuming you're not trying to create a form which can be submitted from an email.
Your form on the website was fine:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body style="padding:3px; margin:0px;" bgcolor="#FFFFFF">
<table cellpadding="0" cellspacing="0" border="0" width="440">
<tr><td style="height:10px"></td></tr>
<tr>
<td colspan="2" style="text-align:justify; line-height:15px;" class="body">
<form name="frm" method="POST" action="careersuccess.php" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td width="23%" class="body"> Name</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strname" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Address</td>
<td width="3%" class="body">:</td>
<td width="74%"><textarea cols="16" name="straddress"></textarea></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> City</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strcity" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> State</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strstate" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Contact No</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="strno" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Email</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="text" name="stremail" class="textfield"></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Comments</td>
<td width="3%" class="body">:</td>
<td width="74%"><textarea cols="16" name="strcomments"></textarea></td>
</tr>
<tr><td style="height:3px"></td></tr>
<tr>
<td width="23%" class="body"> Resume</td>
<td width="3%" class="body">:</td>
<td width="74%"><input type="file" name="strresume"></td>
</tr>
<tr><td style="height:10px"></td></tr>
<tr>
</tr>
</table>
</form>
</td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
</table>
</body>
</html>
Your file careersuccess.php would then need to include something like:
<?php
// multiple recipients
$to = 'demo#localhost.com' . ', '; // note the comma
$to .= 'demo#localhost.com';
// subject
$subject = 'Birthday Reminders for August';
$strName = $_POST["strname"]; //assigns strname from form to PHP variable
$strAddress = $_POST["straddress"];
$strCity = $_POST["strcity"];
$strState = $_POST["strstate"];
$strNumber = $_POST["strnumber"];
$strEmail = $_POST["stremail"];
$strComments = $_POST["strcomments"];
$strResume = $_POST["strresume"];
$lineBreak = "<br />\n\"; //adds carriage return to break up your message
$message = "Name: ".$strName.$lineBreak."Address: ".$strAddress.$lineBreak."City: ".$strCity.$lineBreak."State: ".$strState.$lineBreak...for the rest of your variables;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
That will result in the submitted values of the form being copied in to the email in the $message variable for the mail() function.

Changing php form to submit a styled email (HTML email)

When I submit my form, I receive the email in plain text and not quite in the format I'd like it to be. I'd like it to at least have color to the body and arrange the email so that its neater.
on the contact.php form ... this is what I currently have:
$msg = "You have been contacted by $name with regards to Accommodation.
They passed verification and their message is as follows." . PHP_EOL . PHP_EOL;
$msg .= "$comments" . PHP_EOL . PHP_EOL;
$msg .= "You can contact $name via email, $email or via phone $phone." .
PHP_EOL . PHP_EOL;
$msg .= "We want to stay from the $dayin to the $dayout" . PHP_EOL .
PHP_EOL;
Why is this form using $msg? I've seen in other forms, they use $message.
Can I add CSS styling to the above form?
I also see:
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
Would I need to change "plain" to "HTML"?
Any suggestions or examples would be great!
EDIT:
Ok so now my header are:
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . "\r\n" . PHP_EOL;
$headers .= "Content-type: text/html;charset=iso-8859-1" . "\r\n" .
PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
Do I need PHP_EOL in each header?
My $msg looks like:
$msg = '<style type="text/css">
<!--
.style {
font-family: Arial;
font-size: 12px;
color: #4D241E;
}
body {
background-image: url();
background-color: #F1EAE4;
}
.style1 {font-size: 14px}
-->
</style>
<p> </p>
<table width="420" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td><table width="100%" border="0" cellpadding="8" cellspacing="0" bgcolor="#E7D3AF"
class="style">
<tr>
<td colspan="2" valign="top"><div align="center"><strong><span
class="style1">Submission / Enquiry from your website</span><br>
........................................................................
</strong><br>
</div></td>
</tr>
<tr>
<td width="32%" valign="top"><div align="left"><strong>Date Submitted</strong>
</div></td>
<td width="68%" valign="top">'. date("F j, Y, g:i a") .'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Name</strong></div></td>
<td valign="top">'.$_POST['name'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Email</strong></div></td>
<td valign="top">'.$_POST['email'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Phone</strong></div></td>
<td valign="top">'.$_POST['phone'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Day in</strong></div></td>
<td valign="top">'.$_POST['dayin'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Day out</strong></div></td>
<td valign="top">'.$_POST['dayout'].'</td>
</tr>
<tr>
<td valign="top"><div align="left"><strong>Comments</strong></div></td>
<td valign="top">'.$_POST['comments'].'</td>
</tr>
</table></td>
</tr>
</table>
';
EDIT:
MY NEW HTML EMAIL DESIGN:
<table width="600" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="12" height="14"></td>
<td width="140"></td>
<td width="432"></td>
<td width="16"></td>
</tr>
<tr>
<td height="71"></td>
<td colspan="2" align="center" valign="middle" bgcolor="#F5F5F5"><span style="font-
family:Arial; font-size:18px; color:#333333;"><strong>Submission from</strong>
</span><br />
<span style="font-family:Arial; font-size:16px; color:#f9930f;">
<em>Your website</em></span></td>
<td></td>
</tr>
<tr>
<td height="16"></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Name:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px;
color:#333333;">'.$_POST['name'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Email:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px;
color:#333333;">'.$_POST['email'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Phone:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px;
color:#333333;">'.$_POST['phone'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Day in:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px;
color:#333333;">'.$_POST['dayin'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Day out:</strong></span></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px;
color:#333333;">'.$_POST['dayout'].'</span></td>
<td></td>
</tr>
<tr>
<td height="30"></td>
<td valign="middle"><span style="font-family:Arial; font-size:12px; color:#333333;">
<strong>Comments:</strong></span></td>
<td> </td>
<td></td>
</tr>
<tr>
<td height="190"></td>
<td colspan="2" valign="top"><span style="font-family:Arial; font-size:12px;
color:#333333;">'.$_POST['comments'].'</span></td>
<td></td>
</tr>
<tr>
<td height="20"></td>
<td> </td>
<td> </td>
<td></td>
</tr>
</table>
Now when I add this form in my php and submit my form ... I receive my submission that doesn't look like the table layout like above.
You can use the following headers. These will allow you to just put normal HTML in your message.
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <example#example.com>' . "\r\n";
$headers .= 'Cc: example two#example.com' . "\r\n";
Yep you are going to have to use
Content-type:text/html;charset=iso-8859-1\r\n
It might be a good idea to provide your email in both HTML and text formats as not all email applications support HTML, and many others support it poorly.
To do so use this header instead. The boundary thing is what separates the HTML and text versions in the email, the line below generates a unique code which is suitable for this purpose
$boundary = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/alternative; boundary=$boundary."\"";
Before the text version of the email post this code, note the "--" at the begging. You use the same boundary throughout the email.
--<?php echo $boundary; ?>
Before the HTML version post the same code again
There is a good template you can use here
http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php

Categories