You are not checking if $_POST
is set when they are in the same file and it is an empty array. Add an if statement to check for $_POST
<?php
if($_POST) {
//Receive all the data to process
$contact_name = $_POST['contact_name'] ? : '';
$contact_company = $_POST['contact_company'] ? : '';
$contact_country = $_POST['contact_country'] ? : '';
$contact_phone = $_POST['contact_phone'] ? : '';
$contact_email = $_POST['contact_email'] ? : '';
$contact_subject = $_POST['contact_subject'] ? : '';
$contact_message = $_POST['contact_message'] ? : '';
$contact_headers = 'Sent by: '.$contact_name.' ('.$contact_email.');'.' Phone: '.$contact_phone.'; Country: '.$contact_country.'; Company: '.$contact_company;
$contact_to = 'name@email.com';
if($contact_name != '' || $contact_country != '' || $contact_email != '' || $contact_message != ''){
mail($contact_to, $contact_subject, $contact_message, $contact_headers); //calling php mail function
echo 'Thank you for contacting us.<br><br><a href="contact.php">Go back.</a>';
}else{
echo 'There was an error sending the message.<br><br><a href="index.php">Go back.</a>';
}
}
?>
You should also make sure that you actually have values, the ternary operator is useful for this.
http://us1.php.net/manual/en/function.mail.php
Your headers option in the mail function is not correct. That should be an array, see the example in the manual. It should be:
$contact_headers = 'From: webmaster@example.com';
What you have in your $contact_headers variable should probably get appended to your message. The headers are for your server to send the mail.