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 trialElyas Mansur
1,706 PointsPHP for my contact form not working (email not being delivered)
Hi everyone!
So I've been trying to set up a functional contact form with PHP, however I can't find the problem as to why I'm not receiving the email. Obviously there's something wrong with my PHP code.
I'm new to this and I've spent majority of my day trying to work this out.
please help!
Here's my PHP code
<?php if (isset($_POST["submit"])) { $name = $_POST["name"]; $company = $_POST["company"]; $mailFrom = $_POST["email"]; $phone = $_POST["phone"]; $message = $_POST["message"];
$mailTo = "example@example.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an Email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
And here's my HTML for the contact form
<div class="contact">
<h3>Email Us</h3>
<div class="alert">Your message has been sent</div>
<!-- #TODO! VALIDATION OF EMAIL -->
<form id="contactForm">
<p>
<label>Name</label>
<input type="text" name="name" id="name" required>
</p>
<p>
<label>Company</label>
<input type="text" name="company" id="company">
</p>
<p>
<label>Email Address</label>
<input type="email" name="email" id="email" required>
</p>
<p>
<label>Phone Number</label>
<input type="text" name="phone" id="phone">
</p>
<p class="full">
<label>Message</label>
<textarea name="message" rows="5" id="message"></textarea>
</p>
<p class="full">
<button type="submit" name="submit">Submit</button>
</p>
</form>
</div>
2 Answers
Jake Milburn
13,608 PointsThere are quite a few little things wrong with your code unfortunately but I will try my best to run through the ones I can see and provide you with a working example. Starting with your html. In the form tag you need to specify an action and a method. The action is where the data is being sent to so in this case the name of your php file and the method is how you want to send it so for you it will be post.
In your php file you are checking the status of post correctly however I am unsure about you checking ['submit'], whenever I have dealt with forms upto now I have individually checked each input form so
isset($_POST['name'];
isset($_POST['company'];
However on my forms instead of a button for submitting I use an input with type of submit and a value of submit. You also have not closed that initial if statement with a curly brace that would cause another error. I have never used the mail function but you also seem to have not defined $subject which you are passing it.
Try to run through your code and sort out a couple of these errors and I will comment shortly with a working example that fixes the problem you're having.
Jake Milburn
13,608 Points<div class="contact">
<h3>Email Us</h3>
<div class="alert">Your message has been sent</div>
<!-- #TODO! VALIDATION OF EMAIL -->
<form id="contactForm" action="untitled.php" method="post">
<p>
<label>Name</label>
<input type="text" name="name" id="name" required>
</p>
<p>
<label>Company</label>
<input type="text" name="company" id="company">
</p>
<p>
<label>Email Address</label>
<input type="email" name="email" id="email" required>
</p>
<p>
<label>Phone Number</label>
<input type="text" name="phone" id="phone">
</p>
<p class="full">
<label>Message</label>
<textarea name="message" rows="5" id="message"></textarea>
</p>
<p class="full">
<input type="submit" value="submit">
</p>
</form>
</div>
<?php
$name = $_POST["name"] ?? NULL;
$company = $_POST["company"] ?? NULL;
$mailFrom = $POST["email"] ?? NULL;
$phone = $POST["phone"] ?? NULL;
$message = $POST["message"] ?? NULL;
$mailTo = "jake.milburn@hydrant.co.uk";
$headers = "From: ". $mailFrom;
$txt = "You have received an Email from ". $name . "." . $message;
$subject = "You need to define something for the subject";
mail($mailTo, $subject, $txt);
// header("Location: index.php?mailsend");
You will need to run through and change things like file names. Also check for the email in your spam.
Elyas Mansur
1,706 PointsThank you very much for taking your time to respond so in-depth and correct my mistakes, it is appreciated a lot.
In the next day I'll have a crack at it and comment how I go when I'm done :)