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
Seth Barthen
11,266 PointsHelp with PHPMailer for contact form
So I went through Randy's lesson on how to build a contact form. Mine is slightly different because I'm using bootstrap to layout my form and I don't think I need all the error messages as you can simply use required to make sure the forms are filled out so I got rid a couple of them.
On my computer it shows that the message was sent, but when I try it on a live server it doesn't work... What gives?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["InputName"]);
$email = trim($_POST["InputEmail"]);
$message = trim($_POST["InputMessage"]);
if ($message == "") {
$error_message = "You must specify a message, write to us!";
}
if (isset($error_message)) {
foreach ($_POST as $value) {
if (stripos($value, 'Content-Type:') !== FALSE ) {
$error_message = "There was a problem with the information you entered.";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require ("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer;
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 = "info@example.com";
$mail->AddAddress($address, "Company Name");
$mail->Subject = "Company NameContact 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;
}
}
}
?>
Am I missing some properties in the $mail section? Any help would be appreciated.
It will show this $error_message = "There was a problem sending the email: " . $mail->ErrorInfo; minus the actual error info. So annoying...
3 Answers
Dustin Matlock
33,856 PointsSeth, do you have Sendmail on your server?
Seth Barthen
11,266 PointsThanks for that link. I tried the test page and it worked... Funny thing is I had 15 other pieces of mail waiting for me too... All the ones that gave me the message didn't send.
I'm now wondering if I have my messages jacked up or something. Ugh...
William O'Dell
8,831 PointsIf its working on your box and not the server, then it is probably not setup on the server. If you know the server is setup correctly( via your test script ), then I would do some of the following checks:
- Make sure it's not in your spam folder.
- Make sure you have all of the mailer files pushed to the server.
Maybe those will give you a place to work.
Seth Barthen
11,266 PointsHey William, it ended up working last night and I had a ton of test emails in my companies inbox lol. Is there something wrong in my code above that is giving the error message even when it sends correctly?
William O'Dell
8,831 PointsYou just have your if setup incorrectly. If $mail->Send() == true then it means it was successful. The ! in front of it means that you want to check the opposite of the returned value. So, if $mail->Send() == false is when it goes into your success section.
You just need to remove the !.
if ($mail->Send()) { //If success send to thanks
header("Location: contact.php?status=thanks");
exit;
} else { //Else if fail show message
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
Seth Barthen
11,266 PointsSeth Barthen
11,266 PointsHey Dustin, I'm not quite sure what that is. I'm currently using my buddies cPanel server. Does that help at all? If not let me know and I can dig into this a little deeper. thanks!
Dustin Matlock
33,856 PointsDustin Matlock
33,856 PointsCheck out some of the troubleshooting steps at this link.