PHP Contact Form returning Error Message

2k views Asked by At

I have these exact same PHP contact form codes on 2 different servers. 1 is working as intended, but the other is returning the error message instead of the success message. Also, email is not sent to me.

Contact.php codes as follows:

<?php
ob_start();
 if(isset($_POST['submit']))
 {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';

 $to="[email protected]";
 $subject="Enquiry";
// $headers  = 'MIME-Version: 1.0' . "\r\n";
 //$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "From: ".$email_from."\r\n";
 $message="    
Name:
$name      
Email:
$email     
Message:
$query
";
    if(mail($to,$subject,$message,$headers))
        header("Location: http://www.website.com/contact.php?msg=Successful Submission! Thank you for contacting us."); 
    else
        header("Location: http://www.website.com/contact.php?msg=Error Sending Email!");
 }
?>

I'm wondering if it is permissions/server problem? Any help will be greatly appreciated! Thanks!

1

There are 1 answers

16
Nelson Owalo On BEST ANSWER

Your code construct is correct, and the return value (which is your custom error) might be correct "if" the mail function is failing.

A lot of time we find php mail() function is not working. In some cases, the email is not sent even the function returns true. Following are the points you may check while debugging:

  1. First check if the sendmail_path variable is set properly. You can see it from echo – ing phpinfo() in a page. The typical value is /usr/sbin/sendmail -t -i
  2. If you are sending extra headers in the 4th parameter of the function, try to remove it and send. It is a very common scenario to have incorrect header in the mail function.
  3. Check if you have a spam filter installed in your server which is blocking your email.

If you have access check your mail log file to see if the email is blocked by the mail server or have not reached the server yet. If there is no entry of your email in the log, it means you have problem in your php code or php configuration. If it is in the log, you can see why it was not delivered. The typical path to mail log file is /var/log/maillog