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 trialChristopher Nelson
Front End Web Development Techdegree Student 13,162 PointsEmail form not sending
I am new to PHP and am attempting to set up a form that will send an email when the submit button is pressed. I am completely lost on how to do this. The code below is how I have attempted this task but it doesn't submit an email.
<?php
$emailSubject = 'Photography Inquiry';
$webMaster = 'example@gmail.com';
$Name = $_POST['name'];
$Email = $_POST['email'];
$Type = $_POST['type'];
$Message = $_POST['message'];
$body = <<<EOD
<br><hr><br>
Email: $Email <br>
Name: $Name <br>
Type: $Type <br>
Message: $Message <br>
EOD;
$headers = "From: $Email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<html><body>
<h1 > This Page is currently under maintenance<br>Email me at example@gmail.com </h1>
</body>
</html>
EOD;
echo "$theResults";
?>
1 Answer
Jacob McKinney
6,796 PointsI'd like to point out that you're telling the email that you're providing it with text/html content.
$headers .= "Content-type: text/html\r\n";
But you aren't providing any html. Additionally, there are more headers you should be adding to any email being sent. A failure to do so can end up with your email bouncing to most email providers. The headers you set are dependent on the content of your email, you should check out the mail page on php.net's website to see more about them: http://php.net/manual/en/function.mail.php