Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ashley Carpenter
Ashley Carpenter
13,393 Points

Emails not sending for my contact form

My website is www.pagegardenservices.co.uk

On the enquiries I have a form with some php to send an email, but when I click send i'm not receiving anything. My code is below, can anyone tell my why it's not working?

<?php

if ($_SERVER ["REQUEST_METHOD"] == "POST") {
  $name = $_POST ["name"];
  $email = $_POST ["email"];
  $message = $_POST ["message"];
  $email_body = "";
  $email_body = $email_body . "Name: " . $name . "\n";
  $email_body = $email_body . "Email: " . $email . "\n";
  $email_body = $email_body . "Message: " . $message;

  mail('ashley_carpenter@hotmail.com', "New Message", $email_body);

  header("location: enquiries.php?status=thanks");
  exit;

}

?>


<?php 

$pagetitle = 'Enquiries';

$csslink = 'CSS/Enquiries.css';

include 'include/header.php'; 

?>



<?php if (isset($_GET["status"]) AND $_GET ["status"] == "thanks") { ?>
              <P>Thanks for sending an email. I'll be in touch</P>


          <?php }  else { ?>

      <div>
        <h3 class="grid_12">Send us a Message</h3>

        <div class="form">

          <form action="enquiries.php" Method="POST" Name="enquiry">

            <fieldset>


              <div class="yourinfo">

                <div class="yourname">
                  <label for="YourName">Your Name:</label><br>
                  <input type="text" name="YourName" id="Name"><br><br>
                </div>

                <div class="youremail">
                  <label for="YourEmail">Your Email Address:</label><br>
                  <input type="text" name="YourEmail" id="Email"><br><br>
                </div>

              </div>


              <div class="letusknow">
                <label for="enquiry">Let us know:</label><br>
                <select name="Option">
              </div>


                <optgroup label="Existing Customers">
                  <option>Appointment Times/Dates</option>
                  <option>Payment</option>
                  <option>Special Requests</option>
                </optgroup>
                <optgroup label="New Customers">
                  <option>One off Project</option>
                  <option>Continuous Work</option>
                  <option>Previous Quote</option>
                </optgroup>


              </select>
              <textarea name="enquiry" id="message" rows="10" cols="35"></textarea><br>
              <input class="submit" type="submit" value="Send">


            </fieldset>

          </form>

        </div>
      </div>
      <div>


        <div id="somemadtext">
          <p>Contact us using our online enquiry form at any time. We'll try and contact you back within 48 hours, although the average response time is usually a lot quicker than this.</p><br><br>
          <p>We will be happy to answer any queries whether it is regarding a quote for new work or taking any feedback that you may have.</p>
        </div>
      </div>

      <?php } ?>

<?php 

include 'Include/footer.php';

?>

2 Answers

Where is your site hosted? Are you certain that PHP mail is enabled? If you aren't, try a simple mail test first:

    <?php
    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );
    $from = "emailtest@YOURDOMAIN";
    $to = "YOUREMAILADDRESS";
    $subject = "PHP Mail Test script";
    $message = "This is a test to check the PHP Mail functionality";
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers);
    echo "Test email sent";
    ?>

Replace the values where applicable, upload the test file, and run it, if you receive the e-mail then you will be able to narrow it down to whether it's an issue with your code or not.

Also, you shouldn't have spaces after $_SERVER and $_POST:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = $_POST["name"];
  $email = $_POST["email"];
  $message = $_POST["message"];
  $email_body = "";
  $email_body = $email_body . "Name: " . $name . "\n";
  $email_body = $email_body . "Email: " . $email . "\n";
  $email_body = $email_body . "Message: " . $message;

  mail('ashley_carpenter@hotmail.com', "New Message", $email_body);

  header("location: enquiries.php?status=thanks");
  exit;

}

?>
Ashley Carpenter
Ashley Carpenter
13,393 Points

Hi Mike,

I have uploaded the file (www.pagegardenservice.co.uk/mailtest.php). I ran the page and not received anything.

My website is hosted by GoDaddy.com, do you know how to turn PHP mail on?

Thanks Ashley

You will likely need to contact GoDaddy to see if it is something on their end, perhaps that function is not enabled with your hosting plan.

Hi Ashley,

Does it error? If so can you post the error please?

If not, it may be something to do with the PHP mail function on the server you're using. Randy talks about using a third party library and this can be downloaded via GitHub. At the 7:20 mark here Randy also mentions using a separate mail server over SMTP such as Gmail as the preferred method.

Hope this helps in some way

-Rich