PHP contact form wont send info to email, why?


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

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

if (!preg_match(
$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";

//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');


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


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.


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.


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.


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.