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<noob />
17,062 PointsI dont know what is the problem, have been searching for hours [SOLVED]
I'm keep getting this error when i try to submit the form.
Fatal error: Uncaught Error: Class 'SMTP' not found in C:\Users\admin\Desktop\פרויקט PHP\suggest.php:73 Stack trace: #0 {main} thrown in C:\Users\admin\Desktop\פרויקט PHP\suggest.php on line 73
this is the suggest.php :
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/phpmailer/src/Exception.php';
require 'vendor/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/src/SMTP.php';
//$_POST() is an array that contain all the data that was sent to the server via the "name" tags in the form > username => "what user enters"
//var_dump($_POST);
//we check if the request method is "POST" when we submit the form, and if in our $_POST array we have a value assigned to the key "submit" from the input.
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit"])) {
//stroing the values from the form
$username = trim(filter_input(INPUT_POST,"username",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));
//RECAPTCHE API KEY.
$secretKey = "6LcST70UAAAAAOYamwCOTdhGSeOqulneAprfABBp";
//storing the response key from the server from the $_POST array after i submit the form.
$responseKey = $_POST["g-recaptcha-response"];
//storing the request to the API which will will do with the file_get_contens() func.
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey";
//making a GET request to the api with file_get_contens
//the $reponse var holds the API reponse which is a JSON object that store info about the status of our request, if the user has been fill the captche or not.
$response = file_get_contents($url);
//$response now holds a json string and we want to turn it into a PHP object to get access to the key values pairs.
$response = json_decode($response);
/*after we turn $reponse into a php variable, it now holds an php object that contain key-value pairs.
if the value of the key "success" is true, then the user has been clicked the captche and get verified, if not the user isn't get verified.
var_dump($response);
*/
//checking if a hacker filled the address input and if the name tag have been sent to the server any values we terminate the script.
if($_POST["address"] != "") {
echo "HAHA CAUGHT U HACKER!";
exit;
}
//checking if any of our input field are not filled.
if($username == "" || $email == "" || $details == "" || ($response -> success) === false || !PHPMailer::validateAddress($email)) {
echo "Please fill in all the required Fields!";
exit;
}
$emailBody = "";
$emailBody .= "Username: $username \n";
$emailBody .= "Email: $email \n";
$emailBody .= "Details: $details";
//creating the phpmailer object.
$mail = new PHPMailer;
$mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "emailgaming66@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password is here";
$mail->setFrom('emailgaming66@gmail.com', $username);
$mail->addAddress('emailgaming77@gmail.com');
$mail->Subject = 'Library Suggestion from ' . $username;
$mail->Body = $emailBody;
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
header("location:thanks.php");
}
my files picture: https://gyazo.com/61b8265d02157ad50c3ff46f9ca50007
1 Answer
<noob />
17,062 Points******* I FOUND THE SOLUTION***** This post is for all the people who got blocked with this video because Alena uses a broken code. It took me some time but after research i found the right way to do it.
<?php
//creating the emailBody
$emailBody = "";
$emailBody .= "Username: $username \n";
$emailBody .= "Email: $email \n";
$emailBody .= "Details: $details";
//creating the phpmailer object.
$mail = new PHPMailer(true);
$mail->setFrom($email, $username);
$mail->addAddress('emailgaming66@gmail.com');
$mail->Subject = 'Library Suggestion from ' . $username;
$mail->addReplyTo($email);
$mail->Body = $emailBody;
/* Use SMTP. */
$mail->isSMTP();
//using google smtp server
/* Google (Gmail) SMTP server. */
$mail->Host = 'smtp.gmail.com';
/* SMTP port. */
$mail->Port = 587;
$mail->SMTPDebug = 4;
/* Set authentication. */
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = 'your email will go here';
/* App password. */
$mail->Password = 'your password will go here';
if(!$mail->send()) {
echo $mail->ErrorInfo;
}
header("location:thanks.php");
}