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
Joe Li
3,608 PointsPHPMailer Email not sending
Hi Guys,
I was wondering if somebody could help me?
I have followed Randy's "Adding a Contact Form" videos and I have used PHPMailer for my third party library like Randy suggested and followed the steps exactly.
I uploaded my site and I sent a form to test the validation, everything worked fine. Except I received no email? I sent several forms, it worked once but doesn't seem to work no matter how many I send?
Does anyone know what might be causing this?
Thanks,
Joe
4 Answers
Jack Eccleshall
7,914 PointsHave you checked your junk folder? Mine went into that
James Barnett
39,199 PointsHave you made sure you are using the right mail settings for your host?
Can you post your PHP here as well as which hosting company you are using?
Joe Li
3,608 PointsThe code below is a RSVP for my wedding, so i've got a few inputs you might not typically get in a regular contact form. I'm using dream host as my hosting company. Thanks for your help I really appreciate it!
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim($_POST["name"]); $email = trim($_POST["email"]);
$resultYes = trim($_POST["result_option_YES"]);
$resultNo = trim($_POST["result_option_NO"]);
$transportYes = trim($_POST["transport_option_YES"]);
$transportNo = trim($_POST["transport_option_NO"]);
$veggie_option = trim($_POST["veggie_option"]);
$nutr = trim($_POST["nutr-comment"]);
$comments = trim($_POST["comments"]);
if ($name == "" OR $email == "") {
echo "Please enter your name and email!";
exit;
}
foreach( $_POST as $value) {
if (stripos($value, 'content-type:') !==False){
echo "There was a problem with the info you provided.";
exit;
}
}
if ($_POST["address"] !=="") {
echo "Your form 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 . "Attending: " . $resultYes . $resultNo . "<br>";
$email_body = $email_body . "Transport: " . $transportYes . $transportNo . "<br>";
$email_body = $email_body . "Veggie: " . $veggie_option . "<br>";
$email_body = $email_body . "Nutritional Info.: " . $nutr . "<br>";
$email_body = $email_body . "Comments: " . $comments;
$mail->SetFrom($email, $name);
$address = "joeli1989@gmail.com";
$mail->AddAddress($address, "Joe Li");
$mail->Subject = "RSVP Form | " . $name;
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the form.: " . $mail->ErrorInfo;
exit;
}
header("Location: rsvp.php?status=thanks");
exit;
}
?>
<?php $pageTitle = "RSVP"; include('inc/header.php'); ?>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<div id="thanks" class="grid_12 omega">
<h1>Thank You!</h1>
<p>We have now received your RSVP. You’re all done!</p>
</div>
<?php } else { ?>
<div class="insideDiv">
<div id="intro" class="grid_12 omega">
<h1>We thought we would make life easier for you. Please just fill out the form below and that'll be your RSVP done. If you're a family, please put all the names of everyone attending in the "Name's" section.</h1>
</div>
<div id="rsvp" class="grid_12 omega">
<form method="POST" action="rsvp.php" name="contact-us">
<legend id="dinner_text">RSVP Form</legend><br>
<p class="grid_12 omega">
<label for="name">Name's:</label><br>
<input type="text" name="name" id="name" class="required">
<span>Please enter all names of your party.</span>
</p>
<p class="grid_12 omega">
<label for="email">Email Address:</label><br>
<input type="text" name="email" id="email" class="required">
<span>Please enter a valid email.</span>
</p>
<p class="grid_12 omega">
<label for="result"> Will you be attending our wedding? </label><br><br>
<input type="radio" name="result_option_YES" value="result_option_YES" id="result" class="required"> Yes <br><br>
<input type="radio" name="result_option_NO" value="result_option_NO" id="result" class="required"> No <br>
</p>
<p class="grid_12 omega">
<label for="transport"> Will you require transport form the chapel to the reception? </label><br><br>
<input type="radio" name="transport_option_YES" value="transport_option_YES" id="transport"> Yes <br><br>
<input type="radio" name="transport_option_NO" value="transport_option_NO" id="transport"> No <br>
</p>
<div id="nutr" class="grid_6">
<p>
<label for="veggie">Please select if you require the veggie option instead of the beef for your main course?</label><br><br>
<input type="radio" name="veggie_option" value="veggie_option" id="veggie"> Veggie Option
<p class="nutr">
<label for="nutr-comment">Further Nutritional Info.</label><br>
<textarea name="nutr-comment" rows="10" cols="30" id="nutr-comment"></textarea>
</p>
</div>
<div class="grid_6 omega"><img src="img/rudeveggie.png"></div>
<p id="comments_a">
<label for="comments">Comments</label><br>
<textarea name="comments" rows="10" cols="30" id="comments"></textarea><br>
</p>
<p>
<input id="rsvp_btn" type="submit" value="Send" enabled="disabled"><br>
</p>
<p class="grid_12 omega" style="display: none; ">
<label for="address">Address</label><br>
<input type="text" name="address" id="address">
<span>Please enter all names of your party.</span>
</p>
</form>
</div>
</div>
Joe Li
3,608 PointsHi Guys,
Do you think that it could be that i am using gmail to receive the email? Could it have something to do with SMTP or POP3?
Joe