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

Rex Irvin Carpen
Rex Irvin Carpen
3,314 Points

Fatal error: Uncaught Error: Class 'PHPMailer' not found in /home/treehouse/workspace/suggest.php:21

I think it is because the object used in the video is outdated already can someone help me how to use the new updated PHPMailer?

2 Answers

Sipann A.
Sipann A.
30,234 Points

I've had the same issue. After some research on phpmailer github, I've tried the following and it SEEMS to work (but please keep in mind that I have not been able to test it (as shown in further video) since I'm doing it on my local server. Note that I've added more all .php files from the src folder provided on download to my own inc/phpmailer folder.

=> Added the following at the top the suggest.php file use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;

=> Change what Alena does when she requires the class.phpmailer file by: require("inc/phpmailer/PHPMailer.php"); require("inc/phpmailer/Exception.php"); require("inc/phpmailer/SMTP.php");

$mail = new PHPMailer(true);
if (!$mail -> parseAddresses($email)) {
    echo "Invalid Email Address";
    exit;
}

validateAddress() has been modified as suggested on the phpmailer upgrading documentation.

I'm sure it is far from perfect but as I told you, it seems to work (get the "invalid email address" or "thank you" depending on my input). Hope it helps and let me know if you ended up finding a better solution!

Yohan Park
Yohan Park
7,148 Points

So to add to this... (note date) Above example did not work. I used

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';

with above example on validation.

$mail = new PHPMailer(true);
if (!$mail -> parseAddresses($email)) {
    echo "Invalid Email Address";
    exit;
}

didn't really check if this is how it's supposed to be done or what it's doing. But the form now validates for the email.