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
Jonathan Ripley
Courses Plus Student 1,483 PointsPHP contact form problems
Help :(
Can someone check my code?
I am trying to set up a php contact form. I am basing it on the Mike's T-Shirts site contact form.
I am also trying to incorporate SMTP (I set up an account at Mandrill) based on Randy's recommendation.
FYI - I have the current class.phpmailer.php and class.smtp.php in the inc/phpmailer/ folder.
Can someone look at my code and tell me why it won't work? When I press the submit button, I just get a blank page. I also published it to the web to test it and it doesn't send any emails.
My ISP is Bluehost.
Thanks so much, -Jonathan
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
echo "You must specify a value for name, email address, and message.";
exit;
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
echo "There was a problem with the information you entered.";
exit;
}
}
if ($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
exit;
}
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.mandrillapp.com";
$mail->Port = 587;
$mail->Username = "MY USER NAME";
$mail->Password = "MY PASSWORD";
$mail->SetFrom($email, $name);
$address = "EMAIL I WANT THE FORM RESULTS TO GO TO";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
header("Location: contact.php?status=thanks");
exit;
}
?><?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<p>I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
<tr style="display: none;">
<th>
<label for="address">Address</label>
</th>
<td>
<input type="text" name="address" id="address">
<p>Humans (and frogs): please leave this field blank.</p>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
</div>
</div>
<?php include('inc/footer.php') ?>
3 Answers
Lauren Clark
33,155 PointsHave you tried it on MAMP/WAMP/LAMP etc? Does it send without SMTP? Your code looks fine to me.
Try debugging with
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail - > SMTPDebug = 2; // enables SMTP debug information
// 1 = errors and messages
// 2 = messages only
$mail->Host = "smtp.mandrillapp.com";
$mail->Port = 587;
$mail->Username = "MY USER NAME";
$mail->Password = "MY PASSWORD";
$mail->SetFrom($email, $name);
$address = "EMAIL I WANT THE FORM RESULTS TO GO TO";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
One other thing to try is using port 25 and declaring whether it is SSL or TLS (TLS for port 587, 25 and 2525) (SSL port 465)
I had awful trouble trying to get this to work myself, I had a client's rubbish hosting to deal with, they gave no customisation options, and had overriding files for htaccess, couldn't make text records etc. What I found that worked was using the hosts own mail alias for that domain i.e. smtp.whateverthedomainis.com if there is a mx record set up for your site. I think the crappy hosting was stopping me from using any external SMTP (if that's possible.)
I put in a support ticket and found out which ports weren't blocked and used the host's SMTP, and it works fine now.
Jonathan Ripley
Courses Plus Student 1,483 PointsThanks Lauren!
I'll give those things a shot.
Lauren Clark
33,155 PointsMy thread from before on my broken PHP Contact Form
Some help there maybe, also in the code there is some javascript for alert boxes instead of a blank page, looks a bit neater for errors people might get.
Good luck, let me know how you got on!