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

Anthony Meyer
Anthony Meyer
2,472 Points

Using Object and Validating Email errors with PHPMailer

I'm currently in the PHP track watching the video "Using Object and Validating Email. I've scoured my code and it works if I remove everything from this video, but when it's EXACTLY the same as it is in the video, it errors out.

These are the warning/error regurgitated by the browser and I can't figure them out.

Warning: require(vendor/phpmailer/src/PHPMailer.php): failed to open stream: No such file or directory in /home/treehouse/workspace/suggest.php on line 4

Fatal error: require(): Failed opening required 'vendor/phpmailer/src/PHPMailer.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/treehouse/workspace/suggest.php on line 4

Thanks in advance!

Anthony Meyer
Anthony Meyer
2,472 Points

The following is my code:

<?php 
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/src/Exception.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 == "" || $detials == "") {
        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 think 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="name" 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" />
          <p>Please leave this field blank</p></td>
        </tr>
        </table>
        <input type="submit" value="Send" />
    </form>
    <?php } ?>
    </div>
</div>

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

//The pathway for the mailer is exactly as it shows in the video.

Teacher Russell
Teacher Russell
16,873 Points

HI bro, I just noticed you misspelled the details variable in the second conditional. As for php mailer, I've studied that course, asked for help dozens of times here, scoured the internet, and wasted a week. I have no idea how to get php mailer to work in real life. I have a client waiting on my first website, 3 weeks no. PHP mailer is the only part I can't get to work. Good luck if you actually have to use it. The web is filled with lost souls:)

Anthony Meyer
Anthony Meyer
2,472 Points

@Russeldavis Thanks for that dude! And thank you for the response!

I know we use PHP Mailer at work, but I have no idea how it's set up. My boss built our system from scratch over the past two years.

I've noticed that you're correct about that. No one seems to want to answer questions about PHP. Maybe I'm taking the wrong courses?? I know it's something I'm going to need to learn for work eventually, so I figured it was worth learning now so I could get started. Might have to change my mind on that one.

This MIGHT help with the mailer? https://www.youtube.com/watch?v=fcLXsxpk2dc

Best of luck! I appreciate the help!

1 Answer

Teacher Russell
Teacher Russell
16,873 Points

PHP might not be the most popular thing around now, but it's extremely important and useful. It might be that more people are focused on languages like Python right now. There are some moderators here who are extremely helpful as well as kind and patient, if they see you. Keep it up. PHP is fun, and I think if you master it, there'll always be a demand for you. Just a novice, semi-educated guess, of course. I'll check out the video, and hopefully it's not one of the one's I've already tried to learn from:) Thanks a lot!