Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

PHP Contact form file not working

the mail() function

Moderators: macek, egami, gesf

PHP Contact form file not working

Postby bpascal123 » Fri Aug 10, 2012 11:43 am


I'm new to Php, I understand basic coding things related to variable, loops, functions... more client side. I would like a contact or comments form on my website

The page where the form is is there or link comment on the left window.

I have used the php code from this web site

I made a few changes since I don't want exactly the same thing:

Below is the html (index99_comments.html)

Code: Select all
<div id="gate_A_resize3_comments"><p id="blackboard"></p>

<form method="post" name="contact_form" action="contact-form-handler.php">
Your Name:
<input type="text" name="name">
Email Address:
<input type="text" name="email">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
<input type="submit" value="Submit"> <a href="email_form.php"></a>

And below the php related php code (email_form.php)

Code: Select all

if(isset($_POST['email'])) {
   $email_to = "";
   $email_subject = "Website";
   function died($error) {
      // your error code can go here
      echo "We are sorry, but there were error(s) found in the submitted form. ";
      echo "These errors appear below. <br /><br />";
      echo $error."<br /><br />";
      echo "Please go back and fix these errors. <br /><br />";

   if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['comments'])) {
   died('We are sorry, but a field is missing.');
   $name = $_POST['name'];
   $email = $_POST['email'];
   $comments = $_POST['comments'];
   $error_message = "";
   $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
   if(!preg-match($email_exp, $email_from)) {
      $error_message .= 'The email address is not valid.<br />';
   $string_exp = "/^[A-Za-z .'-]+$/";
   if(!preg_match($string_exp, $name)) {
      $error_message .= 'The name does not seem valid.<br />';
   if(strlen($comments) < 2) {
      $error_message .= 'The comments does not seem valid.<br />';
   if(strlen($error_message) > 0) {
   $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_from)."\n";
   $email_message .= "Comments: ".clean_string($comments)."\n";
   //create email headers
   $headers = 'From: '.$email_from."\r\n".
   'Reply-To: '.$email_from."\r\n".
   'X-Mailer: PHP/' .phpversion();
   @mail($email_to, $email_subject, $email_message, $headers);   
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.


I understand I have to change a few settings on the localhost server for this work locally (I'm using Windows 7 and Xamp). As I want to get quickly to the point, I'm testing it directly online on and it just keeps failing.

Do I have to change settings on the server as well (batcave or tekcities)?
Or is it something about the code?

Thanks if you can provide any clue or answer.

New php-forum User
New php-forum User
Posts: 1
Joined: Fri Aug 10, 2012 11:30 am

Return to PHP coding => Mail

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.