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

Aaron HARPT
Aaron HARPT
19,845 Points

Issue with PHPmailer

I am having an issue with PHPmailer while following the Build A Basic PHP Website course. The specific error I am asking about is that when I send an email, the $email variable and in my case $message (instead of $details) have blank values. Also, the email is coming up as from the name "Root User" and the email "root@localhost.local". It should say that it is from the name and email of the person who sent the email. Here is my code:

<?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));
     $message = trim(filter_input(INPUT_POST, "message", FILTER_SANITIZE_SPECIAL_CHARS));

   if ($name == "" || $email = "" || $message = "") {
     echo "Please fill in all the required fields: Name, Email, and Message";
     exit;
   }

   if ($_POST["address"] != "") {
     echo "Bad form input";
     exit;
   }


// Sending email using PHPMailer
    // requiring phpmailer file
     require("inc/phpmailer/class.phpmailer.php");

    // new PHPMailer object
      $mail = new PHPMailer;

      // Checking if email is valid
      // if (!$mail->validateAddress($email, auto)) {
      //   echo "Invalid Email Address";
      //   exit;
      // }


   $email_body = "";
   $email_body .= "Name " . $name . "\n";
   $email_body .= "Email " . $email . "\n";
   $email_body .= "Message " . $message . "\n";

  //  var_dump($email_body);

  //  $email_body = "";
  //  $email_body .= "Name " . $name . "\n";
  //  // $email
  //  $email_body .= "Email " . "harpt2@icloud.com" . "\n";
  //  // $message
  //  $email_body .= "Message " . "comment" . "\n";

  // $name
   $mail->setFrom($email, $name);
   $mail->addAddress('aaron.harpt@gmail.com', 'Aaron Harpt');     // Add a recipient
   $mail->isHTML(false);                                  // Set email format to HTML

   $mail->Subject = 'Comment on Website from ' . $name;
   // $email_body
   $mail->Body    = $email_body;
  //  $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

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

 }

Thanks, Aaron

1 Answer

First, there is an issue with the if statement towards the top where you are checking if the $name, $email, $message variables are empty. With the $name variable, you are correctly using the double-equal operator (==) to check the value. But for the $email and $message variables you are using the single equal sign (=) which is ASSIGNING the empty string value of "".

if ($name == "" || $email = "" || $message = "") {
     echo "Please fill in all the required fields: Name, Email, and Message";
     exit;
   }