Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Sending HTML form data via E-Mail

I have created a HTML form and want to send its data via email using PHP without the use of outlook etc however, once i have uploaded my site to the server the information isn't sent. Could you please look at my code and advise where i am going wrong please.

<form action="Php/form.php" method="post" name="Contact_Form" >

           <label for="fullname">Full Name: </label> 
           <input type="text" name="Full_Name" id="name"><br>

            <label for="E-Mail">E-Mail: </label>
           <input type="text" name="E-Mail" id="email" ><br>

            <label for="PhoneNumber">Phone Number: </label> 
           <input type="text" name="Phone_Number" id="phonenumber" ><br>

            <label for="Query">Query: </label> 
           <textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>

           <input type="submit" name="submit" value="Send" >


  $emailSubject = 'Website Query';
  $mainEmail = 'r1bos@hotmail.com';

    $nameField = $_POST['Full_Name'];
    $emailField = $_POST['E-Mail'};
    $phoneField = $_POST['Phone_Number'];
    $textField = $_POST ['Query'];

    $body = <<<EOD
Name: $name <br>
Email: $email <br>
Phone Number: $phone <br>
Query: $text <br>

      $headers = "From: $email\r\n";
      $headers .="Content-type: text/html\r\n";  
      $success = mail($mainEmail, $emailSubject, $body, $headers);

          $result = 'Your question has been sent';               
         echo "$result";

1 Answer

Brett Atkin
Brett Atkin
2,574 Points

I see a couple things that look off to me.

The value you're setting for the $body variable looks screwy because I've never used the EOD. Have you tried doing something simple like this to as a test?

$body = "This is the body of the email";

Have you tried not assigning a value to $success and just doing this:

mail($mainEmail, $emailSubject, $body, $headers);

You should probably only set the $result variable if mail() succeeds.

if ($success) { $result = 'Your question has been sent';
echo "$result"; }

You might also try echoing out the variables before calling mail() to make sure all the variables are getting set correctly.

I'm no expert, but hope that helps.