Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Adding a Basic Form Utilizing Object Properties and Methods

An undefined variable is preventing me from continuing with my email form. What's missing?

After filling out the form it gives me an error with the following message:

Notice: Undefined variable: mail in /home/treehouse/workspace/suggest.php on line 24

Fatal error: Uncaught Error: Call to a member function setFrom() on unknown in /home/treehouse/workspace/suggest.php:24 Stack trace: #0 {main} thrown in /home/treehouse/workspace/suggest.php on line 24

Though when I look at the video it is exactly the same.

?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, E-Mail, and Message";
  exit;
}
if ($_POST["address"] != "") {
  echo "Bad form input";
  exit;
}

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

  $email_body = "";
  $email_body .= "Name " . $name . "\n";
  $email_body .= "E-Mail " . $email . "\n";
  $email_body .= "Message " . $details . "\n";

  $mail->setFrom($email, $name);
  $mail->addAddress('treehouse@localhost', 'Alejandro');     // Add a recipient

  $mail->isHTML(false);                                  // Set email format to HTML

  $mail->Subject = 'Personal Media Library Selection from ' . $name;
  $mail->Body    = $email_body;

  if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    exit;
}

header("location:suggest.php?status=thanks");

}

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

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

<div class="section page">
    <h1>Suggest a Media Item</h1>
  <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
      echo "<p>Thanks for the e-mail! I&rsquo;ll check out your suggestion shortly.</p>";

  } else {
  ?>
    <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">E-Mail</label></th>
        <td><input type="text" id="email" name="email"/></td>
      </tr>
      <tr>
        <th><label for="name">Suggest Item Details</label></th>
        <td><textarea name="details" id="details"></textarea></td>
      </tr>
      <tr style="display: none;">
        <th><label for="address">Address</label></th>
        <td><textarea name="address" id="address"></textarea>
        <p>Please leave this field blank</p></td>
      </tr>
    </table>
    <input type="submit" value="send"/>
    </form>
  <?php } ?>
</div>

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

2 Answers

Shane Oliver
Shane Oliver
19,950 Points

Are are never instantiating a new instance of PHPMailer

 require( "inc/phpmailer/class.phpmailer.php" );
$mail = new PHPMailer; // add this line here

Would you know how to set it up so that a specific email address can get it. I put in mine but I continue to get this error

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Shane Oliver
Shane Oliver
19,950 Points

You would need to add a correct email address in the addAddress function

$mail->addAddress('treehouse@localhost', 'Alejandro');     // Add a recipient - this address is not valid

this is just a guess as i haven't looked at PHP in a while but the video suggests that $email is being defined as $email = new phpMailer i think this is needed to create an actual mailer object instance that you can therefore call the setForm() on. i don't see in the above snippet where you are explicitly creating the mailer so add that and see if it gets you further along. Hope this helps.