Escaping Validation and sending form Data to email - php

i have online registeration form, i have done validation for it, but if i submit form without any data,i get validation errors and blank email will be sent to my mail.
Any One Help Me Out
Here is Form Code
<body>
<?php
// define variables and set to empty values
$nameErr = $cnameErr = $mobilenoErr = $emailErr = $cityErr= $postalcodeErr = $addressErr = "";
$name = $cname = $mobileno = $email = $city= $postalcode = $address = "";
$name=$_POST['name'];
$cname=$_POST['cname'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$city=$_POST['city'];
$postalcode=$_POST['postalcode'];
$address=$_POST['address'];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["cname"])) {
$cnameErr = "Company Name is required";
} else {
$cname = test_input($_POST["cname"]);
}
if (empty($_POST["mobileno"])) {
$mobilenoErr = "Mobile Number is required";
}else {
$mobileno = test_input($_POST["mobileno"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[789][0-9]{9}$/",$mobileno)) {
$mobilenoErr = "Not A Valid Number";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["city"])) {
$cityErr = "City is required";
} else {
$city = test_input($_POST["city"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "Only letters and white space allowed";
}
}
if (empty($_POST["postalcode"])) {
$postalcodeErr = "Postal Code is required";
} else {
$postalcode = test_input($_POST["city"]);
}
if (empty($_POST["address"])) {
$addressErr = "Address is required";
} else {
$address = test_input($_POST["address"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(!empty($_POST['name'])&& !empty($_POST['cname'])&&!empty($_POST['mobileno'])&& !empty($_POST['email'])&&!empty($_POST['city'])&&!empty($_POST['postalcode'])&&!empty($_POST['address'])){
header('Location: Submission.php?$submit=1');
exit();}
$to = 'sskumbar7#gmail.com';
$subject = 'I need to show html';
$from ='sandeep.sk#microvillage.in';
ini_set("sendmail_from", $from);
$headers = "From: " .$from. "\r\n";
$headers .= "Reply-To: ".$from. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body ='User Name:'.$name.'<br>';
$body .= 'Company Name:'.$cname.'<br>';
$body .= 'Mobile Numbaer:'.$mobileno.'<br>';
$body .= 'Email Id:'.$email.'<br>';
$body .= 'City:'.$city.'<br>';
$body .= 'Postalcode:'.$postalcode.'<br>';
$body .= 'Address:'.$address.'<br>';
$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
$headers .= "Content-type: text/html\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Sent</p>");
} else {
echo("<p>Error...</p>");
}
?>
<div class="gridContainer clearfix">
<div id="div1" class="fluid"><!-- header ends here-->
<div id="header" class="fluid">
<div class="fluid logo_container zeroMargin_tablet">
<div class="fluid logo_mvc"></div>
<!-- logo_mvc ends here-->
<div class="fluid logo_gsm"></div>
<!-- logo_gsm ends here-->
</div>
<!-- logo_container ends here-->
</div>
<div class="fluid imageslide zeroMargin_desktop">
<div class="fluid imageslide_gs zeroMargin_desktop"></div>
<!-- imageslide_gs ends here-->
<div class="fluid imageslide_content">
<h1>IP Product Introduction and VoIP PBX
Appliance Training Day # Toronto</h1>
</div><!-- imageslide_content ends here-->
<div class="fluid imageslide_product"></div>
<!-- imageslide_product ends here-->
</div><!-- imageslide ends here-->
<div class="fluid content">
<div class="fluid content_det">
<h3>Event information</h3>
<p>Please join us at the Fairfield Inn & Suites Toronto Airport where Grandstream will offer four different sessions during the day. </p>
<h3>Introduction to Grandstream IP products</h3>
<p><b>8:45am - 10:15am</b><br/>
Introduction to Grandstream, and basic information on Grandstream products including ATAs, gateways, routers and telephones. </p>
<h3>Introduction to IP cameras IP and Surveillance products</h3>
<p><b>12:45pm - 2:15pm</b><br/>
Basic information on IP cameras and surveillance products, and the introduction of the brand new GVR3550 Network Video Recorder. </p>
<h3>Advanced Technical Training for UCM VoIP PBX's</h3>
<p><b>2:30pm - 4:30pm</b><br/>
This session will focus on the advanced features of the UCM series, including the new features of the upcoming software and the brand new UCM6510 VoIP PBX for T1 networks. </p>
</div><!-- content_det ends here--><div class="fluid contet_form">
<h2>Register Now</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="fluid div_form"><label><b>First name *:</b></label>
<input type="text" size="20px" name="name" placeholder="Enter Your Name Here" value="<?php echo $name; ?>"/><span class="error"><?php echo $nameErr;?></span>
</div>
<div class="fluid div_form"><label><b>Company Name *:</b></label>
<input type="text" size="20px" name="cname" placeholder="Enter Your Company Name Here" value="<?php echo $cname; ?>"/><span class="error"><?php echo $cnameErr;?></span></div>
<div class="fluid div_form"><label><b>Mobile Number *:</b></label>
<input type="text" size="20px" name="mobileno" placeholder="Enter Your Mobile Number Here" value="<?php echo $mobileno; ?>"/><span class="error"><?php echo $mobilenoErr?></span>
</div>
<div class="fluid div_form"><label><b>Email Id *:</b></label>
<input type="email" size="20px" name="email" placeholder="Enter Your Email Id Here" value="<?php echo $email; ?>"/><span class="error"><?php echo $emailErr?></span></div>
<div class="fluid div_form"><label><b>City *:</b></label>
<input type="text" size="20px" name="city" placeholder="Enter Your City Name Here" value="<?php echo $city;?>"/><span class="error"><?php echo $cityErr?></span></div>
<div class="fluid div_form"><label><b>Postal Code *:</b></label>
<input type="text" size="20px" name="postalcode" placeholder="Enter Postal Code Here" value="<?php echo $postalcode; ?>"/><span class="error"><?php echo $postalcodeErr?></span>
</div>
<div class="fluid div_form"><label><b>Address *:</b></label>
<input type="text" size="20px" name="address" placeholder="Enter Address Here" value="<?php echo $address; ?>"/><span class="error"><?php echo $addressErr?></span></div>
<button name="submit" >Submit</button>
</form>
</div><!-- contet_form ends here-->
</div><!-- content ends here-->
</div><!-- div1 ends here-->
</div>
</body>

Remove this if
if(!empty($name)&& !empty($cname)&&!empty($mobileno)&& !empty($email)&&!empty($city)&&!empty($postalcode)&&!empty($address)){
and place
if(!empty($_POST['name'])&& !empty($_POST['cname'])&&!empty($_POST['mobileno'])&& !empty($_POST['email'])&&!empty($_POST['city'])&&!empty($_POST['postalcode'])&&!empty($_POST['address'])){
$to = 'sskumbar7#gmail.com';
$subject = 'I need to show html';
$from ='sandeep.sk#microvillage.in';
ini_set("sendmail_from", $from);
$name=$_POST['name'];
$cname=$_POST['cname'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$city=$_POST['city'];
$postalcode=$_POST['postalcode'];
$address=$_POST['address'];
$headers = "From: " .$from. "\r\n";
$headers .= "Reply-To: ".$from. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body ='User Name:'.$name.'<br>';
$body .= 'Company Name:'.$cname.'<br>';
$body .= 'Mobile Numbaer:'.$mobileno.'<br>';
$body .= 'Email Id:'.$email.'<br>';
$body .= 'City:'.$city.'<br>';
$body .= 'Postalcode:'.$postalcode.'<br>';
$body .= 'Address:'.$address.'<br>';
$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
$headers .= "Content-type: text/html\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Sent</p>");
} else {
echo("<p>Error...</p>");
}
header('Location: Submission.php?submit=1');
}

move following variable from if condition and put them above if condition
$name=$_POST['name'];
$cname=$_POST['cname'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$city=$_POST['city'];
$postalcode=$_POST['postalcode'];
$address=$_POST['address'];
and replace if(isset($name)){
with if(!empty($name)){

Related

My Email code is not working when i am replacing

When i am replacing the email with my email id, it shows "?ok" and if i am leaving it default it shows "Your emails was Sent Successfully"
i guess there is something wrong with php code, when i edit it shows "?ok", and when i reset it back it agains shows "?ok" until i don't upload the original file which i edited (it has same code, just not opened or edited)
Here is my Php code:
<?php
// Replace this with your own email address
$siteOwnersEmail = 'user#website.com';
if($_POST) {
$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check Name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>
HTML CODE:
<div class="contact-primary">
<h3 class="h6">Send Us A Message</h3>
<form name="contactForm" id="contactForm" method="post" action="" novalidate="novalidate">
<fieldset>
<div class="form-field">
<input name="contactName" type="text" id="contactName" placeholder="Your Name" value="" minlength="2" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactEmail" type="email" id="contactEmail" placeholder="Your Email" value="" required="" aria-required="true" class="full-width">
</div>
<div class="form-field">
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" class="full-width">
</div>
<div class="form-field">
<textarea name="contactMessage" id="contactMessage" placeholder="Write us a summary for your business and what we can do to help" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
</div>
<div class="form-field">
<button class="full-width btn--primary">Submit</button>
<div class="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
</fieldset>
</form>
<!-- contact-warning -->
<div class="message-warning">
Something went wrong. Please try again.
</div>
<!-- contact-success -->
<div class="message-success">
Your message was sent, thank you!<br>
</div>
</div> <!-- end contact-primary -->
Please suggest what are the changes i need to do to make work.
you can check here how error shows https://freesoft64.com/test.html

PHP - Mail script (Select element)

I have been working on a website recently from a website template (Puremedia). The template originally had a working contact form. It consisted of "First name", "last name", "Email" and "Subject". And then of course the message. I have now replaced the "Last name" text-box with a dropdown menu. I have done so by using the html select tags. Now my question is how I would change the php scripting so that instead of sending the message (Contact form) to One predefined email. It can send it to two different email adresses. How I would like it to be implemented would be using the dropdown menu. So for example the dropdown menu would be someling along these lines: "Choose Receiver" , "Mail 1" , "Mail 2".
And then also adding a checkbox below the message field that will send a copy to the persons own email address. - Here is my contact form code:
<form name="contactForm" id="contactForm" method="post" action="">
<fieldset>
<div class="row">
<div class="six columns mob-whole">
<label for="contactFname">First Name <span class="required">*</span></label>
<input name="contactFname" type="text" id="contactFname" placeholder="First Name" value="" />
</div>
<div class="six columns mob-whole">
<select name="mailacc">
<option value="1">Send til (Vælg venligst)</option>
<option value="2">Direktør</option>
<option value="3">Bogholder</option>
</select>
</div>
</div>
<div class="row">
<div class="six columns mob-whole">
<label for="contactEmail">Email <span class="required">*</span></label>
<input name="contactEmail" type="text" id="contactEmail" placeholder="Email" value="" />
</div>
<div class="six columns mob-whole">
<label for="contactSubject">Subject</label>
<input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" />
</div>
</div>
<div class="row">
<div class="twelve columns">
<label for="contactMessage">Message <span class="required">*</span></label>
<textarea name="contactMessage" id="contactMessage" placeholder="Your Message" rows="10" cols="50" ></textarea>
</div>
</div>
<div>
<button class="submit full-width">Send Message</button>
<div id="image-loader">
<img src="images/loader.gif" alt="" />
</div>
</div>
</fieldset>
</form> <!-- /contactForm -->
<!-- message box -->
<div id="message-warning"></div>
<div id="message-success">
<i class="fa fa-check"></i>Your message was sent, thank you!<br />
</div>
</div> <!-- /contact-form -->
The PHP part:
<?php
// Replace this with your own email address
$siteOwnersEmail = 'mail#domain.com';
if($_POST) {
$fname = trim(stripslashes($_POST['contactFname']));
$lname = trim(stripslashes($_POST['contactLname']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));
// Check First Name
if (strlen($fname) < 2) {
$error['fname'] = "Please enter your first name.";
}
// Check Last Name
if (strlen($lname) < 2) {
$error['lname'] = "Please enter your last name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+#[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
$error['message'] = "Please enter your message. It should have at least 15 characters.";
}
// Subject
if ($subject == '') { $subject = "Contact Form Submission"; }
// Set Name
$name = $fname . " " . $lname;
// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
// Set From: header
$from = $name . " <" . $email . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (!$error) {
ini_set("sendmail_from", $siteOwnersEmail); // for windows server
$mail = mail($siteOwnersEmail, $subject, $message, $headers);
if ($mail) { echo "OK"; }
else { echo "Something went wrong. Please try again."; }
} # end if - no validation error
else {
$response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
$response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
echo $response;
} # end if - there was a validation error
}
?>

Some PHP variables not appearing when contact us form is used

below is the php code for my contact form, the website is live at the moment and when i use the form to send a query using the contact us form, not all information gets through:
In the email that i receive, the only information that comes through is $subject and the message ($name would like to move in on $move.\r\n\n";). No other data that the client inputs on the website is listed in the email. I have looked at the code and cant seem to find what is wrong.
I would appreciate any help.
<?php
if(!$_POST) exit;
$to = 'mydomain#email.com';
$name = $_POST['txtname'];
$email = $_POST['txtemail'];
$phone = $_POST['txtphone'];
$comp = $_POST['txtcomp'];
$emp = $_POST['txtemp'];
$move = $_POST['txtmove'];
$comment = $_POST['txtmessage'];
if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }
$subject = 'You\'ve been contacted by ' . $name . '.';
$msg = "You have been contacted by $name.\r\n\n";
$msg .= "$comment\r\n\n";
$msg .= "You can contact $name via email, $email.\r\n\n";
$msg = "You can call $name on $phone.\r\n\n";
$msg = "$name has $emp employees and the company name is $comp.\r\n\n";
$msg = "$name would like to move in on $move.\r\n\n";
$msg .= "-------------------------------------------------------------------------------------------\r\n";
if(#mail($to, $subject, $msg, "From: $email\r\nReturn-Path: $email\r\n"))
{
echo "<span class='success-msg'>Thanks for Contacting Us, We will call back to you soon.</span>";
}
else
{
echo "<span class='error-msg'>Sorry your message was not sent, Please try again.</span>";
}
?>
Thanks for the replies and suggestions, below is the HTML for the form:
<div id="map"></div>
<div class="dt-sc-margin50"></div>
<div class="container">
<div class="column dt-sc-three-fourth first">
<div class="hr-title">
<h3>Request A Call Back</h3>
<div class="title-sep">
</div>
</div>
<form method="post" class="dt-sc-contact-form" action="php/send.php" name="frmcontact">
<div class="column dt-sc-one-third first">
<p> <span class="auto-style2">Your Name</span><span> <input type="text" placeholder="Name*" name="txtname" maxlength="25" value="" required /> </span> </p>
</div>
<div class="column dt-sc-one-third">
<p><span class="auto-style2">Your Email Address<span> <input type="email" placeholder="Email*" name="txtemail" value="" required /> </span> </p>
</div>
<div class="column dt-sc-one-third">
<p><span class="auto-style2">Your Contact Number <span> <input type="text" placeholder="Phone" name="txtphone" value="" /> </span> </p>
</div>
<div class="column dt-sc-one-third first">
<p><span class="auto-style2">Company Name <span> <input type="text" placeholder="Company Name" name="txtcomp" value="" /> </span> </p>
</div>
<div class="column dt-sc-one-third">
<p><span class="auto-style2">No Of Employees <span> <input type="number" placeholder="No Of Employees" name="txtemp" value="" /> </span> </p>
</div>
<div class="column dt-sc-one-third">
<p><span class="auto-style2">Move In Date <span> <input type="date" placeholder="Move In Date" name="txtmove" value="" /> </span> </p>
</div>
<p> <span class="auto-style2">Please describe below what kind of office you are looking for,we will reply to your query on the same day.<textarea placeholder="Message*" name="txtmessage" maxlength "750" required ></textarea> </p>
<p> <input type="submit" value="Send Message" name="submit" /> </p>
</form>
<div id="ajax_contact_msg"></div>
</div>
Update 01/10/15
Hi Guys
I made the changes suggested by Dp and sebastianbrosch. The website is now live again but when i attempt to send the form, it says "the page save failed". Below is the updated php code, the html contact us page file remains the same.
Hi apologies i have pasted the php code, but for some reason, it is not showing up in the post. I will try again.
<?php
if(!$_POST) exit;
$to = 'mydomain#email.com';
$name = $_POST['txtname'];
$email = $_POST['txtemail'];
$phone = $_POST['txtphone'];
$comp = $_POST['txtcomp'];
$emp = $_POST['txtemp'];
$move = $_POST['txtmove'];
$comment = $_POST['txtmessage'];
if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }
$subject = 'Office enquiry via domain.com from ' . $name . '.';
$msg = "You have been contacted by ".$name."\r\n\n";
$msg .= "You can contact ".$name." via email, ".$email.".\r\n\n";
$msg .= "You can call ".$name." on ".$phone.".\r\n\n";
$msg .= "$name has ".$emp." employees and the company name is ."$comp.".\r\n\n";
$msg .= $name." would like to move in on ."$move.".\r\n\n";
$msg .= $comment."\r\n\n";
$msg .= "---------------------------------------------------------------\r\n";
if(#mail($to, $subject, $msg, "From: $email\r\nReturn-Path: $email\r\n"))
{
echo "<span class='success-msg'>Thanks for Contacting Us, We have received your query and will be in touch soon.</span>";
}
else
{
echo "<span class='error-msg'>Sorry your message was not sent, Please try again or contact us via live chat.</span>";
}
?>
There are missing the points in front of the = in the following lines
$msg = "You can call $name on $phone.\r\n\n";
$msg = "$name has $emp employees and the company name is $comp.\r\n\n";
$msg = "$name would like to move in on $move.\r\n\n";
Replace with the following lines
$msg .= "You can call $name on $phone.\r\n\n";
$msg .= "$name has $emp employees and the company name is $comp.\r\n\n";
$msg .= "$name would like to move in on $move.\r\n\n";
Your Code :
$msg = "You have been contacted by $name.\r\n\n";
$msg .= "$comment\r\n\n";
$msg .= "You can contact $name via email, $email.\r\n\n";
$msg = "You can call $name on $phone.\r\n\n";
$msg = "$name has $emp employees and the company name is $comp.\r\n\n";
$msg = "$name would like to move in on $move.\r\n\n";
Try The Following :
$msg = "You have been contacted by ".$name."\r\n\n";
$msg .= $comment."\r\n\n";
$msg .= "You can contact ".$name." via email, ".$email.".\r\n\n";
$msg .= "You can call ".$name." on ".$phone.".\r\n\n";
$msg .= "$name has ".$emp." employees and the company name is $comp.".\r\n\n";
$msg .= $name." would like to move in on ."$move.".\r\n\n";
I think you had several string concatenation errors according to what you have explained in your question.

sending form info to email

I have a form that sends info to php doc. When testing, I submit and array displays but won't go to email address.
<?php require_once("/includes/fbcheader.php");
?>
<div class="comments">
<h3> We Welcome All Comments </h3>
<form action="/php/contact-send.php" method="post"
id="contact-form" class="form">
<p class="input-block">
<label for="form-name" >Name</label>
<input type="text" value name="name" id="form-name"
autofocus placeholder="Please enter name" required>
</p>
<p class="input-block">
<label for="form-email" >Email</label>
<input type="email" value name="email" id="form-email"
required placeholder="Please enter E-Address">
<input type="hidden" value name="age" id="age">
</p>
<p class="input-block">
<label for="form-subject" >Subject</label>
<input type="text" value name="subject" id="form-subject"
required placeholder="Please enter subject">
</p>
<p class="textarea-block">
<label for="form-message">Comment</label>
<textarea name="message" id="form-message"
cols="70" rows="10"></textarea>
</p>
<div class="clear"></div>
<input type="hidden" name="firstname" id="firstname">
<input type="submit" value="Send" id="form-submit">
<p class="hide" id="response"></p>
<div class="hide">
<label for="spam-check">Do not fill out this field</label>
<input name="spam-check" type="text" value id="spam-check">
</div>
</form>
</div>
</body>
</html>
This is the sending php execute code
<html>
<head>
<title>Contact-Send</title>
</head>
<body>
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($email = $_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($email = $_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($message = $_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_from = '$Email';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "me#myaddress.com";
?>
<br>
<?php
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $form_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("thank-you.html");
?>
</body>
</html>
On submit I simply get the array displaying the content of the input fields
A quick look at your PHP code I see the following:
This: if(null!==($email = $_POST["email"])) will fail because you are running a check against nothing. it should be: if(null!==($_POST["email"])).
You have an undefined variable here: $email_from = '$Email'; this will simply output "$Email". It should be $email_from = $email; (as per your code). However you are simply re-assigning the variable here which you dont really need to be doing at all.
Your concatenating the $email_body and $to address.
You header("thank-you.html"); will fail because as per your code, output has already started here <?php (line 6).
You should be aiming for something closer to this for your contact-send.php:
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "me#myaddress.com";
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: thank-you.html");
?>
Remember to set $to to your email address!

Failing to send form data to email

Hi all i have designed a page which has online registration form, i want filled form data to be sent to email id, for that i have used below code,
$to = 'example#example.com';
$subject = 'I need to show html';
$from ='from#from.com';
$body = '<p style=color:red;>This text should be red</p>';
ini_set("sendmail_from", $from);
$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
$headers .= "Content-type: text/html\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Sent</p>");
} else {
echo("<p>Error...</p>");
}
I have hosted this using free web hosting to test, everything works fine but submitted details doesnt come mentioned email id, i am really new to this ,can some one help me out
Here is the Form Code:
<body>
<?php
// define variables and set to empty values
$nameErr = $cnameErr = $mobilenoErr = $emailErr = $cityErr= $postalcodeErr = $addressErr = "";
$name = $cname = $mobileno = $email = $city= $postalcode = $address = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["cname"])) {
$cnameErr = "Company Name is required";
} else {
$cname = test_input($_POST["cname"]);
}
if (empty($_POST["mobileno"])) {
$mobilenoErr = "Mobile Number is required";
}else {
$mobileno = test_input($_POST["mobileno"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[789][0-9]{9}$/",$mobileno)) {
$mobilenoErr = "Not A Valid Number";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["city"])) {
$cityErr = "City is required";
} else {
$city = test_input($_POST["city"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "Only letters and white space allowed";
}
}
if (empty($_POST["postalcode"])) {
$postalcodeErr = "Postal Code is required";
} else {
$postalcode = test_input($_POST["city"]);
}
if (empty($_POST["address"])) {
$addressErr = "Address is required";
} else {
$address = test_input($_POST["address"]);
}
}
$to = 'example#Example.com';
$subject = 'I need to show html';
$from ='from#from.com';
$body = '<p style=color:red;>This text should be red</p>';
$body .= '<label>User Name:</label>'.$name.'<br>';
$body .= '<label>Company Name:</label>'.$cname.'<br>';
ini_set("sendmail_from", $from);
$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
$headers .= "Content-type: text/html\r\n";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Sent</p>");
} else {
echo("<p>Error...</p>");
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(!empty($name)&& !empty($cname)&&!empty($mobileno)&& !empty($email)&&!empty($city)&&!empty($postalcode)&&!empty($address)){
header('Location: Submission.php?$submit=1');
exit();}
?>
<div class="gridContainer clearfix">
<div id="div1" class="fluid"><!-- header ends here-->
<div id="header" class="fluid">
<div class="fluid logo_container zeroMargin_tablet">
<div class="fluid logo_mvc"></div>
<!-- logo_mvc ends here-->
<div class="fluid logo_gsm"></div>
<!-- logo_gsm ends here-->
</div>
<!-- logo_container ends here-->
</div>
<div class="fluid imageslide zeroMargin_desktop">
<div class="fluid imageslide_gs zeroMargin_desktop"></div>
<!-- imageslide_gs ends here-->
<div class="fluid imageslide_content">
<h1>IP Product Introduction and VoIP PBX
Appliance Training Day # Toronto</h1>
</div><!-- imageslide_content ends here-->
<div class="fluid imageslide_product"></div>
<!-- imageslide_product ends here-->
</div><!-- imageslide ends here-->
<div class="fluid content">
<div class="fluid content_det">
<h3>Event information</h3>
<p>Please join us at the Fairfield Inn & Suites Toronto Airport where Grandstream will offer four different sessions during the day. </p>
<h3>Introduction to Grandstream IP products</h3>
<p><b>8:45am - 10:15am</b><br/>
Introduction to Grandstream, and basic information on Grandstream products including ATAs, gateways, routers and telephones. </p>
<h3>Introduction to IP cameras IP and Surveillance products</h3>
<p><b>12:45pm - 2:15pm</b><br/>
Basic information on IP cameras and surveillance products, and the introduction of the brand new GVR3550 Network Video Recorder. </p>
<h3>Advanced Technical Training for UCM VoIP PBX's</h3>
<p><b>2:30pm - 4:30pm</b><br/>
This session will focus on the advanced features of the UCM series, including the new features of the upcoming software and the brand new UCM6510 VoIP PBX for T1 networks. </p>
</div><!-- content_det ends here--><div class="fluid contet_form">
<h2>Register Now</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="fluid div_form"><label><b>First name *:</b></label>
<input type="text" size="20px" name="name" placeholder="Enter Your Name Here" value="<?php echo $name; ?>"/><span class="error"><?php echo $nameErr;?></span>
</div>
<div class="fluid div_form"><label><b>Company Name *:</b></label>
<input type="text" size="20px" name="cname" placeholder="Enter Your Company Name Here" value="<?php echo $cname; ?>"/><span class="error"><?php echo $cnameErr;?></span></div>
<div class="fluid div_form"><label><b>Mobile Number *:</b></label>
<input type="text" size="20px" name="mobileno" placeholder="Enter Your Mobile Number Here" value="<?php echo $mobileno; ?>"/><span class="error"><?php echo $mobilenoErr?></span>
</div>
<div class="fluid div_form"><label><b>Email Id *:</b></label>
<input type="email" size="20px" name="email" placeholder="Enter Your Email Id Here" value="<?php echo $email; ?>"/><span class="error"><?php echo $emailErr?></span></div>
<div class="fluid div_form"><label><b>City *:</b></label>
<input type="text" size="20px" name="city" placeholder="Enter Your City Name Here" value="<?php echo $city;?>"/><span class="error"><?php echo $cityErr?></span></div>
<div class="fluid div_form"><label><b>Postal Code *:</b></label>
<input type="text" size="20px" name="postalcode" placeholder="Enter Postal Code Here" value="<?php echo $postalcode; ?>"/><span class="error"><?php echo $postalcodeErr?></span>
</div>
<div class="fluid div_form"><label><b>Address *:</b></label>
<input type="text" size="20px" name="address" placeholder="Enter Address Here" value="<?php echo $address; ?>"/><span class="error"><?php echo $addressErr?></span></div>
<button name="submit" >Submit</button>
</form>
</div><!-- contet_form ends here-->
</div><!-- content ends here-->
</div><!-- div1 ends here-->
</div>
</body>
And here Submission.php Code
<?php
$submit=1;
if( !isset($_POST[$submit]) )
{
}
else{
header('Location:index.php');
exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Thank You For Registration</title>
</head>
<body>
<h4>Registration Sucessfull</h4><br/>
Go To Registration Page
</body>
</html>
First The problem is its an html email so use correct headers for it and then send the form data which you want in the body of html file
Test with this and let me know whether its working
$to = 'from#from.com';
$subject = 'I need to show html';
$from ='from#from.com';
ini_set("sendmail_from", $from);
$username=$_POST['name'];
$mobilenum=$_POST['mobileno'];
$cname=$_POST['cname'];
$address=$_POST['address'];
$postalcode=$_POST['postalcode'];
$headers = "From: " .$from. "\r\n";
$headers .= "Reply-To: ".$from. "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body = '<p style=color:red;>This text should be red</p>';
$body.='<label>User Name:</label>'.$username.'<br>';
$body .= '<label>Mobile Num:</label>'.$mobilenum.'<br>';
$body .= '<label>Company Name:</label>'.$cname.'<br>';
$body .= '<label>Address:</label>'.$address.'<br>';
$body .= '<label>Postalcode:</label>'.$postalcode.'<br>';
if (mail($to, $subject, $body, $headers)) {
echo("<p>Sent</p>");
} else {
echo("<p>Error...</p>");
}

Categories