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

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:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

//console.log("Nope");

$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.";
    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("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();

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

$mail->isSendmail();

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

$mail->SetFrom($email, $name);
$address = "example@gmail.com";
$mail->AddAddress($address, "First Last");
$mail->Subject    = "First Last 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;
}
?>

1 Answer

I would take a look at the require file -

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

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