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

Build an Interactive Website > Form Validation and Manipulation

I'm using easyPHP DevServer

Here is a look at the contact.php file from the project files:

 <?php

 $to_email = "bakeon@smellslikebakin.com";

 $name = $_POST["name"];
 $email = $_POST["email"];
 $message = $_POST["message"];

 $subject = "[Contact Form] " . $_POST["subject"];


 function isValidEmail($email) {
     return strpos($email, "@") !== false;
 }

 if($name != "" && $email != "" && $message != "" && isValidEmail($email)) {

     $full_message = "Name : ". $name . "\n";
     $full_message .= "Email : ". $email . "\n";
         $full_message .= "Message :\n\n". $message . "\n";

     mail($to_email, $subject, $full_message);
     header("location: contact.html");
     exit();
}

 die("Your data is invalid.");


 ?>

I changed: $to_email = "bakeon@smellslikebakin.com"; to: $to_email = "(my email)";

I'm using easyPHP development server and everything looks correct on the form. However, when I wrote a message and submitted the form on localhost, I expected to receive an email but nothing happened. How do I get my forms to actually email the message?

1 Answer

Good Morning. Do you have any data from your systems server error logs? This could be a sendmail/smtp configuration issue from your local development environment. I would start in the server logs.

Thanks Hampton. This is what my error log looks like:

 [Fri Sep 27 09:37:36.516434 2013] [:error] [pid 4524:tid 944] [client 127.0.0.1:51777] PHP Warning:    
 mail(): &quot;sendmail_from&quot; not set in php.ini or custom &quot;From:&quot; header missing in  
 C:\\web\\contact.php on line 22, referer: http://localhost/web/contact.html

I'm new to Apache (and working with servers in general) so I'm not completely clear on what this means.

Okay I think I have things figured out. First, I had to add a header to my contacts.php:

    $header = "From: someone@somewhere.com\r\n";
mail($to_email, $subject, $full_message, $header);

Then I had a new error: PHP Warning: mail(): Failed to connect to mailserver...

I'm just testing on /localhost and I found out that my WAMP doesn't include a mail server, so I downloaded smtp4dev which lets me test that the mail is getting sent out.

Now it seems to be working

Great job Ryan.