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

Victor Albarran
Victor Albarran
4,662 Points

Html form and php to send to mail

I am building a website and want to add a form I did it in html and wanted to use php to retrieve the information via email.

so far I had the code and tested my server provider if support php and it does but when filling the form and submit it, i do not receive an email.

here is my code:

html

<h3>Request a Quote</h3> <p></p>

 <form action="mail_handler.php" method="post">

First Name: <input type="text" name="first_name"><br> Last Name: <input type="text" name="last_name"><br> Email: <input type="text" name="email"><br> Message:<br><textarea rows="5" name="message" cols="30"></textarea><br> <input type="submit" name="submit" value="Submit"> </form>

and php

<?php if(isset($_POST['submit'])) { $to = "vanportcleaners@gmail.com"; // this is your Email address $from = $_POST['email']; // this is the sender's Email address $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $subject = "Form submission"; $subject2 = "Copy of your form submission"; $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message']; $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";

}

?>