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

Not receiving emails with PHP form.

Hello, I have been trying to get emails from the website form, for some reason I am not receiving the emails.

My code for the form is below:

        <form action="contact-process.php" method="post" name="cnt">

        <div class="form-group">

            <div class="input-group">

                    <span class="input-group-addon"><span class="glyphicon glyphicon-user form-icons"></span></span>
                    <label class="sr-only">Name</label>
                    <input class="form-control" type="text" placeholder="Name" name="name" required type="text" id="cu_name" >

            </div>

        </div>

        <div class="form-group">

            <div class="input-group">

                    <span class="input-group-addon"><span class="glyphicon glyphicon-envelope form-icons"></span></span>
                    <label class="sr-only">Email</label>
                    <input class="form-control" type="email" placeholder="Email" name="email" required id="cu_email" >

            </div>

        </div>

        <div class="form-group">

            <div class="input-group">

                    <span class="input-group-addon"><span class="glyphicon glyphicon-earphone form-icons"></span></span>
                    <label class="sr-only">Phone</label>
                    <input class="form-control" type="text" placeholder="Phone" name="phone" required  id="cu_phone">

            </div>

        </div>

        <div class="form-group">

            <div class="input-group">

                    <span class="input-group-addon"><span class="glyphicon glyphicon-pencil form-icons"></span></span>
                    <textarea class="form-control" rows="8"  placeholder="Message" name="msg" id="cu_msg"></textarea>

            </div>

        </div>

        <button type="submit" name="cu_sub" id="cu_sub" name="submit"  value="SUBMIT" class="btn btn-primary btn-lg btn-block form-btn">Submit</button>


        </form>

` ` `

This is my php code:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$toemail = "abc505@yahoo.com";
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$msg = $_POST["msg"];
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Phone: " . $phone ."\n";
$email_body = $email_body . "Message: " . $msg;


//TODO: Send Email


header("Location: contact-thanks.php");
exit;
}
?>


Please help

1 Answer

Hey Arshdeep,

In your PHP code, you have //TODO: Send Email. So it looks like you're missing the code that actually sends the email. You should go back and watch the videos again, or continue watching the videos to make sure that you get all of the code you need to make the form work.

Exactly. You've created the email body, but not the actual process of sending. Randy covers this in a few videos time. If you're looking to skip ahead, you will find what you're looking for in the badge called "Wrapping up the project" here. It's the very last badge.

I recommend not skipping because there's some great stuff that goes on inbetween.

Also be aware that if you're working on a local server, email functionality may not work.

Thank you Kevin, I had completely missed the later videos, Its all working now. Cheers