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

Sharon Roth
Sharon Roth
7,754 Points

PHP contact form

HELP! I'm trying to make a simple contact form, but I am not very familiar with back-end code. Can someone please please PLEASE help me figure out why this isn't working? I modified it from a template I found online. Below is the relevant html and php.

HTML <div id="form-messages"></div>

     <form name="contactform" method="post" action="send_form_email.php"> <!--id="ajax-contact" method="post" action="mailer.php"> current option from http://www.freecontactform.com/email_form.php-->
      <div class="form-group row field">
        <label for="name" class="col-sm-2 col-form-label">Name:</label>
        <div class="col-sm-6 col-lg-4">
          <input type="text" class="form-control" id="name" placeholder="Your Full Name" name="name" required>
        </div>
      </div>
       <div class="form-group row field">
        <label for="email" class="col-sm-2 col-form-label">Email:</label>
        <div class="col-sm-6 col-lg-4">
          <input type="email" class="form-control" id="email" placeholder="youremail@example.com" name="email" required>
        </div>
      </div>
       <div class="form-group row field">
        <label for="message" class="col-sm-2 col-form-label">Message:</label>
        <div class="col-md-8">
          <textarea class="form-control" id="message" rows="6" placeholder="Type your message" name="message" required></textarea>
        </div>
      </div>
         <div class="field">
            <input type="submit" class="btn btn-primary d-block m-x-auto" value="Send Message">
         </div>
    </form>

  </div>

And the php <?php

if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "email@email.com";

$email_subject = "New message";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['message'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

}



$name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$message = $_POST['message']; // required



$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 you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$name)) {

$error_message .= 'The Name you entered does not appear to be valid.<br />';

}

if(strlen($message) < 2) {

$error_message .= 'The Comments you entered do not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$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($message)."\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);

?>

<?php

}

?>

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

what are the error messages said ?

Sharon Roth
Sharon Roth
7,754 Points

The error message that I get is: Cannot POST /send_form_email.php

Also, in the top of the php file I have my actual email filled in. Just didn't want to share it here, but instead of email@email.com I put my actual email.

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

ah you mean the form you fill is not delivered to the email destination ?

Sharon Roth
Sharon Roth
7,754 Points

Exactly, when I click the submit button on my webpage, it goes to blank page with the Cannot POST /send_form_email.php error message, and no email is actually sent.

Ideally, I'd like it to send and then clear the form.

1 Answer

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

You can't send message to your actual email from your local environment, you need real server and SMTP (Server Mail Transfer Protocol) in order to send message .

Sharon Roth
Sharon Roth
7,754 Points

Thanks! Do you have any suggestions for how to have a functioning contact form? Can I set up a SMTP? Or is there another way around that?

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

sorry i never use SMTP before but you can check tutorial on google, google is your friend :) !