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!

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

PHP Build a Simple PHP Application Wrapping Up The Project Using A Third-Party Library

Nuri Amari
Nuri Amari
8,212 Points

Using PHP mailer object isn't working.

I am trying to adapt the form processing from this tutorial to my own project. However, all I can do using phpmailer without throwing an error is creating a $mail object. Any code manipulating this object throws the following error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Here is the php code springing the error:




$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.";

foreach( $_POST as $value ){
    if( stripos($value,'Content-Type:') !== FALSE ){
        echo "There was a problem with the information you entered.";    

if ($_POST["address"] != "") {
    echo "Your form submission has an error.";

$mail = new PHPMailer();

/*Any code relating to the $mail object doesn't work below here.*/


if (!$mail->ValidateAddress($email)){
    echo "You must specify a valid email address.";


$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;

$mail->SetFrom($email, $name);
$address = "example@gmail.com";
$mail->AddAddress($address, "First Last");
$mail->Subject    = "First Last Contact Form Submission | " . $name;

if(!$mail->Send()) {
  echo "There was a problem sending the email: " . $mail->ErrorInfo;

header("Location: contact.php?status=thanks");

1 Answer

I would take a look at the require file -


Is this definitely the right location? If you're using shirts4mike as a base, have you established the ROOT_PATH constant yet? I would add this onto the front of your require:

require_once(ROOT_PATH . "inc/phpmailer/class.phpmailer.php");

If you haven't done this part of the course yet (it might even be in the next course), just make sure you're 100% pointing to the right file.

Also check your .htaccess (if you have one) and look really closely for syntax errors.

Have you tried checking the $mail object? You could take a look at this check: http://php.net/manual/en/function.get-class-methods.php to see if the object is successfully created.

Last but not least, grab the project files from the video course. If you swap your code for the project files and it's still not working, then you know the issue isn't on this page. If it starts working, you will be able to better pinpoint which piece isn't working.

Hope this helps