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 trialAaron Munoz
11,177 PointsNeed Help with my code for Extra Credit for Enhancing PHP Application
This is what I came up with. I noticed that I don't get an error message for when "There was a problem sending the email." It just reloads the contact form with the values. First off, did I approach the extra credit the right way? And second, why won't it give me a confirmation that it sent nor give me an error?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
$error_message = array();
if ($name == "") {
$error_message [] = "You must specify a value for name.";
}
if ($email == ""){
$error_message [] = "You must specify a valid email address.";
}
if ($message == "") {
$error_message [] = "You must include a message.";
}
if (!isset($error_message)) {
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom($email, $name);
$address = "aaron@theblueocean.tv";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if ($mail->Send()) {
header("Location: contact.php?status=thanks");
exit;
} else {
$error_message [] = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p>I’d love to hear from you! Complete the form to send me an email.</p>';
} else { foreach($error_message as $error) {
echo '<p class="message">' . $error . '</p>';
}
} ?>
Brandon Berger
3,947 PointsBrandon Berger
3,947 PointsPhp mail function will almost always return true, it cannot tell if the server sent the email successfully and the user received it