PHP contact form wont send info to email, why?


#1
<?php

$errors = ‘’;
$myemail = ‘my email here’;//<-----Put Your email address here.
if(empty($_POST[‘name’]) ||
empty($_POST[‘email’]) ||
empty($_POST[‘message’]))
{
$errors .= “\n Error: all fields are required”;
}

$name = $_POST[‘name’];
$email_address = $_POST[‘email’];
$message = $_POST[‘message’];

if (!preg_match(
“/^[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/i”,
$email_address))
{
$errors .= “\n Error: Invalid email address”;
}

if( empty($errors))
{
$to = $email_address;
$email_subject = "Contact form submission from: $name ";
$email_body = "You have received a new message. “.
" Here are the details:\n Name: $name \n Email: $email_address \n Message: \n $message”;

$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');

}
?>

Contact form handler <?php echo nl2br($errors); ?>

#2

Are you really expecting the user to enter YOUR email address in the form in order to get it to you? You would have less problems if you didn’t create variables for no reason.


#3

are you referring to this section: $myemail = ‘my email here’;//<-----Put Your email address here

No they don’t have to enter their email that is where the message is being email to when they hit the submit button. I just didn’t put my email in the section yet.


#4

The variable in the mail function that sends YOU the email is set to $to. $to is a pointless variable assignment from $email_address. $email_address is a pointless variable for $_POST[‘email’]. $_POST[‘email’] is either a form field supplied by the user or it is a hidden field hard coded with who knows what.

You also have the reply to set to whatever the user enters for an email. So the user should reply to themselves?

Is it clear now as to why you are not getting an email?

The whole code is a mess and vulnerable to an email injection attack.


#5

I knew something was wrong. This was given to me and it is obvious that they don’t understand php language. Thank you for your help.