First of all am the beginner please help me out.
I have used the following code in php for sending a email directly from my page but it is not working.
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$email = $_POST['email'];
$contact_no = $_POST['contact_no'];
//Validate first
if(empty($name)||empty($email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($email))
{
echo "Bad email value!";
exit;
}
$email_from ='forms@stockshasthra.in';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $contact_no".
$to = "narayanan@stockshasthra.in";//<== update the email address
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-youl');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
The error is after submitting is:
error; you need to submit the form!
Warning: Cannot modify header information - headers already sent by (output started at D:\INETPUB\VHOSTS\stockshasthra.in\httpdocs\form-to-email.php:5) in D:\INETPUB\VHOSTS\stockshasthra.in\httpdocs\form-to-email.php on line 35
Thanks in advance
