1

I am trying to get my email form to work and having a problem figuring out why it does not go threw to my email address. When i hit submit i get directed to the thank you page as if the form was sent correctly but i never receive the email. I don't know how to debug this to figure out what i am doing wrong and it is really bugging me that i am stuck on a measly form problem. In case you cant tell from my code i am using Foundation for my framework.

Any help would be greatly appreciated, thank you.

Here is my html

<div class="section4">
            <div class="row">
                <div class="small-12 medium-6 columns">
                    <form data-abide method="post" name="myemailform" action="FormToEmail.php"> 
                        <h2>Get in Touch</h2>
                        <div class="row">
                            <div class="large-8 columns">
                                <label>Name <small>required</small>
                                    <input type="text" name="name" placeholder="Your Name" required pattern="[a-zA-Z]+"/>
                                </label>
                                <small class="error">Name is required and must be a string.</small>
                            </div>
                        </div>
                        <div class="row">
                            <div class="large-8 columns">
                                <label>Email <small>required</small>
                                    <input type="text" name="email" placeholder="Your Email" required/>
                                </label>
                                <small class="error">An email address is required.</small>
                            </div>
                        </div>
                        <div class="row">
                            <div class="large-8 columns">
                                <label>Company
                                    <input type="text" name="company" placeholder="Your Company" />
                                </label>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="small-12 medium-6 columns">
                    <h2>Don't Hesitate to Leave a Message</h2>
                    <div class="row">
                        <div class="large-12 columns">
                            <label>Message
                                <textarea name="message" placeholder="Your Message Here"></textarea>
                            </label>
                        </div>
                    </div>
                    <input type="submit" name="submit" class="submit right" value="submit" />
                </div>
            </div>
        </div><!-- END OF SECTION4 -->

AND HERE IS MY FormToEmail.php file

<?php
   if(!isset($_POST['submit']))
 {
     //This page should not be accessed directly. Need to submit the form.
     echo "error; you need to submit the form!";
 }
 $name = $_POST['name'];
 $visitor_email = $_POST['email'];
 $company = $_POST['company'];
 $message = $_POST['message'];

 //Validate first
  if(empty($name)||empty($visitor_email)) 
   {
    echo "Name and email are mandatory!";
    exit;
   }  

 if(IsInjected($visitor_email))
   {
    echo "Bad email value!";
    exit;
   }

  $email_from = $visitor_email;//<== update the email address
  $email_subject = "New Form submission";
  $email_body = "You have received a new message from the user $name.\n".
      "Here is the message:\n $message".

  $to = "theller5567@gmail.com";//<== update the email address
  $headers = "From: $email_from \r\n";
  $headers .= "Reply-To: $visitor_email \r\n";
  //Send the email!
  mail($to,$email_subject,$email_body,$headers);
  //done. redirect to thank-you page.
  header('Location: thank-you.html');

  // Function to validate against any email injection attempts
  function IsInjected($str)
  {
   $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
           );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
     return true;
    }
   else
     {
      return false;
     }
   }

   ?> 
Travis Michael Heller
  • 1,210
  • 3
  • 16
  • 34
  • 1
    PHP's `mail()` function uses the SMTP configuration in your `php.ini` file. Check with this file or ask your webhost how SMTP is configured on your server. Many low-end webhosts do not provide SMTP services because of the risk of spam, in which case you'll need to use a third-party SMTP service. – Dai Oct 29 '14 at 03:12
  • 1
    try to debug what is returning from `mail()`. If you get `false`, the mail sending failed. – Sithu Oct 29 '14 at 03:13
  • 1
    There is one default configuration problem in XAMPP 1.8.0 and `mail()` http://stackoverflow.com/a/13955025/1179841 – Sithu Oct 29 '14 at 04:08

0 Answers0