Error on contact form!

Dodge

Regular Member
Joined
Jul 29, 2009
Messages
389
Reaction score
53
Hi there,

I know nothing about coding/PHP, and am running into an error message when trying to use the contact form included on my website. I've been unable to get in touch with the web designer, and would really appreciate if someone could help out - I'm presuming the error is something that can be easily fixed.

When a form is submitted it comes up with the error: "An error occured. Please try again later" - I have no idea why!

I have included the full mail.php code here, I'd really appreciate if anyone can advise me on how to fix.

Thanks

<?php

/* =====================================================
* change this to the email you want the form to send to
* ===================================================== */
$email_to = "[email protected]";
$email_subject = "Contact Form submitted";


if(isset($_POST['email']))
{


function return_error($error)
{
echo $error;
die();
}


// check for empty required fields
if (!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message']))
{
return_error('Please fill in all required fields.');
}


// form field values
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$contact_number = $_POST['contact_number']; // not required
$message = $_POST['message']; // required


// form validation
$error_message = "";


// name
$name_exp = "/^[a-z0-9 .\-]+$/i";
if (!preg_match($name_exp,$name))
{
$this_error = 'Please enter a valid name.';
$error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
}


$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp,$email))
{
$this_error = 'Please enter a valid email address.';
$error_message .= ($error_message == "") ? $this_error : "<br/>".$this_error;
}


// if there are validation errors
if(strlen($error_message) > 0)
{
return_error($error_message);
}


// prepare email message
$email_message = "Form details below.\n\n";


function clean_string($string)
{
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}


$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Contact number: ".clean_string($contact_number)."\n";
$email_message .= "Message: ".clean_string($message)."\n";


// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (@mail($email_to, $email_subject, $email_message, $headers))
{
echo 'Form submitted successfully.';
}
else
{
echo 'An error occured. Please try again later.';
die();
}
}
else
{
echo 'Please fill in all required fields.';
die();
}
?>
 
It might be two reasons. 1, you don't have the PHP mail function enabled. 2, PHP has no way of sending mail because you don't have sendmail installed.

Are you hosting this on a web host or VPS?

What operating system if it's a VPS?
 
if (@mail($email_to, $email_subject, $email_message, $headers))
Remove the @ character and you should see the real error message
 
An error occured. Please try again later is not an error message. It is just something that your script prints for you if the mail function returns false. You should indeed remove the '@' from (@mail($email_to, $email_subject, $email_message, $headers)). If it still returns false you most likely don't have an smtp server to relay your email.
 
check iptables or try to disable the service for 2 minutes to see if this is the problem
 
also check the mail logs in /var/log - in 99% that the solution found in the logs
 
Back
Top