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
Benjamin Ku
6,720 PointsHaving problems with a PHP mailer
Hi there,
I'm currently in the process of making my own site, and have been trying to get a php mailer form working. I've been following this tutorial (http://blog.teamtreehouse.com/create-ajax-contact-form) to try and make an ajax contact form, but I've been having problems. I've got the form fields set up in the html and I've copied the app.js file into the folder as is. I included the mailer.php file (which I modified to have my information) and set that as an action in my html form.
So this issue is, when I click the submit button on the form, it gives me an "Internal Server Error" and I have no idea why. It looks like the email is sending properly too as I have been receiving my test emails, so I'm not sure why it's sending me to an internal server error page.
I've attached the code I'm using below:
'''html <form id="ajax-contact" method="post" action="mailer.php" class="eight columns"> <div class="field"> <input type="text" id="name" name="name" placeholder="Name" autocomplete="off" required> </div> <div class="field"> <input type="email" id="email" name="email" placeholder="Email" autocomplete="off" required> </div> <div class="field"> <textarea id="message" name="message" placeholder="Message" autocomplete="off" required></textarea> </div> <div class="field"> <button type="submit">Send It</button> </div> </form> '''
'''php <?php // My modifications to mailer script from: // http://blog.teamtreehouse.com/create-ajax-contact-form // Added input sanitizing to prevent injection
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "contact@myemail.com";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?> '''
'''js $(function() {
// Get the form.
var form = $('#ajax-contact');
// Get the messages div.
var formMessages = $('#form-messages');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#message').val('');
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
});
}); '''
So to clarify, when I click the submit button, my browser goes to mailer.php page and displays a message saying "Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log."
It is still sending the email. What I want it to do is stay on the same page and just display a small text message saying the email has been sent.
Any help would be appreciated, I seriously haven't a clue bout what to do next.
Cheers,
-Ben
thomascawthorn
22,986 PointsI've had great success with the PHP track Mike Baxter suggests.
Have you tried running the same code without the html_response_code functions? I've only taken a quick look but if you're getting an internal server error, it might be worth taking a look at the areas where you're mentioning the server i.e. html response codes and maybe the .htaccess file if you have one?
Mike Baxter
4,442 PointsMike Baxter
4,442 PointsMy guess is, your server may not be configured correctly. I've never set these options myself, but I know there can be issues with them. (For one thing, I think you have to set the address your server is mailing from, probably in a config file.)
Maybe try using the plugin they use in the PHP track instead? http://teamtreehouse.com/library/build-a-simple-php-application/wrapping-up-the-project/using-a-thirdparty-library