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 Basic PHP Website (2018) Adding a Basic Form Using Object and Validating Email

S Ananda
S Ananda
9,474 Points

Class 'PHPMailer' not found in /home/treehouse/workspace/suggest.php:17

Can't see an error in my code. Have there been changes to the github files that I need to be aware of?

Complete error message: Fatal error: Uncaught Error: Class 'PHPMailer' not found in /home/treehouse/workspace/suggest.php:17 Stack trace: #0 {main} thrown in /home/treehouse/workspace/suggest.php on line 17

<?php 


if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
    $details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

    if ($name == "" || $email =="" || $details == "") {
      echo "Please fill in the required fields: Name, Email and Details";
      exit;
    }
    if ($_POST["address"] != "") {
      echo "Bad form input";
      exit;
    }
  if (!PHPMailer::validateAddress($email)){
    echo "Invalid Email Address";
    exit;
  }

    echo "<pre>";
      $email_body = "";
      $email_body .= "Name " . $name . "\n";
      $email_body .= "Email " . $email . "\n";
      $email_body .= "Details " . $details . "\n";
    echo $email_body;
    echo "</pre>";

    //To Do: Send email
    header("location:suggest.php?status=thanks");
}

$pageTitle = "Suggest a Media Item";
$section = "suggest";

include("inc/header.php"); ?>

<div class="section page">
  <div class="wrapper">
    <h1>Suggest a Media Item</h1>
    <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
        echo "<p>Thanks for the email! I&rsquo;ll check out your suggestion shortly!</p>";
     } else { ?>
    <p> If you thnk there is something I&rsquo;m missing, let me know! Complete the form to send me an email.</p>
    <form method="post" action="suggest.php">
      <table>
        <tr>
          <th><label for="name">Name</label></th>
          <td><input type="text" id="name" name="name" /></td>
        </tr>
        <tr>
          <th><label for="email">Email</label></th>
          <td><input type="text" id="email" name="email" /></td>
        </tr>
        <tr>
          <th><label for="details">Suggest Item Details</label></th>
          <td><textarea name="details" id="details"></textarea></td>
        </tr>
        <tr style="display:none">
          <th><label for="address">Email</label></th>
          <td><input type="text" id="address" name="address" />Please leave this field blank.</td>
        </tr>
      </table>
      <input type="submit" value="Send" />

    </form>
    <?php } ?>
  </div>
</div>

<?php include("inc/footer.php"); ?>

At a glance, you appear to be missing the use and require lines.

Hope this helps!

2 Answers

As Peter Smart said, you need to include the use and require statements. Assuming that you you were able to successfully download the PHPMailer zip folder from the github repository then upload those files to the vendor folder that you would have created, you want to add the following statements at the top of the page (right after <?php):

<?php
use PHPMAILER\PHPMAILER\PHPMAILER;
require 'vendor/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/src/Exception.php';

I hope this helps!

S Ananda
S Ananda
9,474 Points

Thank you #Peter Smart and #Mister Moody. That was my problem. Sorry it took so long to respond. I had a busy work weekend and just now got a chance to check it out. Now I'm ready to move on in the course.

No worries. Keep coding!