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 trialmeabh mattimoe
Courses Plus Student 467 PointsPHP Contact Form
Hi Team,
I was hoping you could help me out with this one. I'm trying to set up a Contact Form for my own personal site. I've been following the course 'Build a Simple PHP Application' and I've completed the lesson 'Sending the Contact Form Email'. After this I am completely stuck. I just want to test receiving an email, I can't seem to receive the email but the thank you form pops up when I click the send button, indicating that the email has been received, but when I check my mail nothing has been sent. I have downloaded the latest version of PHP mailer from github also because notes were added to the lesson as it has been updated. My question is where do I go from here? I just want to test that I can receive an email. I can't follow the updated notes it's confusing. I haven't got a domain name yet and is it possible to test receiving an email on MAMP? My code is below. Would really appreciate your help!! Thankyou
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$company = trim($_POST["company"]);
$phone = trim($_POST["phone"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $company == "" OR $phone == "" OR $email == "" OR $message == "") {
echo "You must specity a value for name, company, phone, 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("include/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 . "company: " . $company . "<br>";
$email_body = $email_body . "phone: " . $phone . "<br>";
$email_body = $email_body . "email: " . $email . "<br>";
$email_body = $email_body . "message: " . $message;
$mail->SetFrom($email, $name);
$address = "meabh76@yahoo.com";
$mail->addAddress($address, "Volcano Design");
$mail->Subject = "Volcano Design 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";
$section = "contact";
include('include/header.php');
?>
<section class="sheet">
<section class="wrap">
<h1>GET IN TOUCH</h1><br>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<h1>THANKYOU!</h1><br>
<p>We will be in touch shortly!</p>
<?php } else { ?>
<p>We'd love to hear from you!<br>Complete the form to send us your query and we will be in touch right away!</p>
<div class="formWrap">
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Full Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="company">Company Name</label>
</th>
<td>
<input type="text" name="company" id="company">
</td>
</tr>
<tr>
<th>
<label for="phone">Phone</label>
</th>
<td>
<input type="text" name="phone" id="phone">
</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">Enquiry</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>
<div class="button">
<input type="submit" value="send message">
</div>
</form>
<?php } ?>
</div>
</section>
</section>
2 Answers
Julian Aramburu
11,368 PointsHi! First of all... are you testing your script in a live server or in a localhost, like your own pc/mac? Because of the latter you need to set a mail server in your php files (iirc in your php.ini) in order to properly send mails. You will need the smtp data from the mail server and complete that data in your php.ini, otherwise you would not be able to test it locally.
Hope it helps!
meabh mattimoe
Courses Plus Student 467 PointsHi Julian thank you for your help!! It worked thanks so much!!!!