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
Joseph Hall
10,865 PointsPHP Contact Form issue on live site
Dear Treehouse,
I am working on a live website for my father (I am only 13, so this is my first!) Some of the website is not finished quite yet, but when I try to fill out the contact form, no email is sent so my Dad can see it when someone contacts him.
The live site: cnsbiomed.com Click on the Contact Us link in the navigation mene
The code:
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=edge">
<link href="contact/css/contactform.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools-yui-compressed.js"></script>
<script type="text/javascript" src="contact/validation/validation.js"></script>
<script type="text/javascript">
var nameError = '<?php echo $error_messages['fullname']; ?>';
var emailError = '<?php echo $error_messages['email']; ?>';
var commentError = '<?php echo $error_messages['comment']; ?>';
</script>
<?php
// Set email variables
$email_to = 'c.hall@cnsbiomed.com';
$email_subject = 'Contact Form Submission from website';
// Set required fields
$required_fields = array('fullname','email','comment');
// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
</head>
<body onload="MM_preloadImages('contact/images/x.png')">
<div id="formWrap">
<p style="text-align: center;"><img src="logo.png" /></p>
<div id="form">
<?php if($form_complete === FALSE): ?>
<h2 style="text-align: center;">Please fill out the form below to contact us.</h2>
<form action="contact.php" method="post" id="comments_form">
<div class="row">
<div class="label">Your Name</div> <!-- end .label -->
<div class="input">
<input type="text" id="fullname" class="detail" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" /><?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>
</div><!-- end .input -->
<div class="context">E.g. John Smith or AnyTown Medical Center</div><!-- end .context -->
</div><!-- end .row -->
<div class="row">
<div class="label">Your Email Address</div> <!-- end .label -->
<div class="input">
<input type="text" id="email" class="detail" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" /><?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?> </div><!-- end .input -->
<div class="context">CNSBiomedical will never sell or share your information.</div><!-- end .context -->
</div><!-- end .row -->
<div class="row">
<div class="label">Your Message</div> <!-- end .label -->
<div class="input2">
<textarea id="comment" name="comment" class="mess">
<?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?></textarea><?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?> </span><?php endif; ?>
</div><!-- end .input -->
</div><!-- end .row -->
<div class="submit">
<input type="submit" id="submit" name="submit" value="Send Message" class="button" />
</div><!-- end .submit -->
<?php else: ?>
<p class="thank">Thank you for contacting us! CNSBiomedical will respond to you within 24 hours.</p>
<script type="text/javascript">
setTimeout('Redirect()', 3000); //after 3 seconds, redirect back to home page ("index.html")
function Redirect()
{
location.href='../index.html';
}
</script>
<?php endif; ?>
</form>
</div><!-- end#form -->
</div><!-- end #formWrap -->
</body>
</html>
Can you tell me why the email is not sending correctly??
10 Answers
James Barnett
39,199 Points@Joseph Hall -Moral of the Story It's always safer to have a dev version of a site and test all your changes their and then push that known good code to your production site.
Chase Lee
29,275 PointsDid you remember to put in the extension .php instead of .html.
Joseph Hall
10,865 PointsYes it is .php
Joseph Hall
10,865 PointsAs shown in the URL. cn
Chase Lee
29,275 PointsOkay I'm sorry, I can't help you anymore I have no experience in "php".
Joseph Hall
10,865 PointsOk thanks for the try! I am going to tag @Randy Hoyt with this one
Chase Lee
29,275 PointsI'll do it. Randy Hoyt please help.
Joseph Hall
10,865 PointsThanks!
Matt Grubb
2,804 PointsAll in all, the code itself should be working from what I see, except some servers do require certain headers (ie. from) to be present. Have you tried adding the "From:" header to the mail function?
mail($to, $subject, $message, "From: Sender Name <email@domain.com>");
More information on basic headers for the mail function here
Thanks, Matt.
Joseph Hall
10,865 PointsHey Matt Thanks for the suggestion.
Here is the code with the new "From"
$to = "c.hall@cnsbiomed.com";
$subject = "Contact form submission from CNSBioMedical Website";
// if validation passed ok then send the email
mail($to, $subject, $email_content, "From:" . $email);
The funny thing is, after the form is submitted with correct data, it should display a thank you message, which happens when using MAMP. However, the output after the form is submitted on the live website does not show any html output/markup:
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=edge">
<link href="contact/css/contactform.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools- yui-compressed.js"> </script>
<script type="text/javascript" src="contact/validation/validation.js"></script>
<script type="text/javascript">
var nameError = 'Please fill in the Name field to proceed';
var emailError = 'Please enter a valid email address to proceed';
var commentError = 'Please fill in the message area so we can best help you with your request';
</script>
Randy Hoyt Please come help!!
Matt Grubb
2,804 PointsThat's really awkward, lol. I took the script you originally posted, and tested it on my VPS, and it does, in fact, show the success message, as well as send the email. I'm actually curious as to what is causing the issue, so as soon as you know, please let us know!
Joseph Hall
10,865 PointsOk sure thing Mat!
Thanks for your help!
My dad is using yahoo small business for web hosting, so that may be the issue. I'll give them a call once w get back from Nova Scotia
Matt Grubb
2,804 PointsYeah, I just searched yahoo small business hosting, seems they do have a lot of restrictions when it comes to php, etc. I'd defiantly suggest giving them a call.
Sorry I was unable to help you with your problem!
Joseph Hall
10,865 PointsNo problem! By actually testing it yourself, you helped a ton!
Joseph Hall
10,865 PointsJoseph Hall
10,865 PointsAlright moral taken!