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 Introducing Dependency Management and Composer

JONATHAN MOO
JONATHAN MOO
9,224 Points

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

After installing phpmailer via composer, when I try to enter a new email through the form, it shows up the fatal error above.

I have tried 'composer update' to install but the same error ensues.

This is happening in workspaces. How do I solve this error?

10 Answers

Krishna Diamesso
Krishna Diamesso
33,880 Points

Make sure when you are installing phpmailer to specify version 5.2.patch (5.2.2 for example) with version 5.2 which is what Alena is using $mail = new PHPMailer works but with the newer version of phpmailer it seems you have to use $mail = new PHPMailer\PHPMailer\PHPMailer();

Hero, this still works.

Glenré Charl Labuschagné
Glenré Charl Labuschagné
23,204 Points

Hi guys, Krishna Diamesso is right.

If you want to follow the exact steps with Alena be sure to use phpmailer v.5.2.2. In the command line instead of leaving the version number blank and just pressing enter, type 5.2.2

Review the 'composer.json' file to see the version number… If it's not "phpmailer/phpmailer": "5.2.2", simply change it and delete your 'vendor' folder and run 'composer update' to install the correct version.

Brooke Adrienne
Brooke Adrienne
2,539 Points

instead of: $mail = new PHPMailer

try using: $mail = new PHPMailer\PHPMailer\PHPMailer();

Worked for me, solution found here: https://stackoverflow.com/questions/46059612/uncaught-error-class-phpmailer-not-found

Hi there,

Just wanted to see if anyone had figured out the error mentioned by JONATHAN MOO? Also giving me the Class 'PHPMailer' not found error.

Any help would be great,

Thanks in advance!

David Marec
David Marec
9,578 Points

Hi there

I've the same fatal error. Is there anybody found the solution ?

Thanks for help. David.

Ray Knag
Ray Knag
8,110 Points

I am having the same error and replacing the $mail variable is not working as some have indicated. I think the issue may be with applying an update to composer, when I try to run composer update I get this:

treehouse:~/workspace$ composer update
Warning: This development build of composer is over 60 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

If I try to run "/usr/local/bin/composer self-update" I get:

treehouse:~/workspace$ /usr/local/bin/composer self-update
Updating to version 1.6.5 (stable channel).
Downloading (100%) [ErrorException]
rename(/home/treehouse/.composer/cache/composer-temp.phar,/usr/local/bin/composer): Permission denied

self-update [-r|--rollback] [--clean-backups] [--no-progress] [--update-keys] [--stable] [--preview] [--snapshot] [--set-channel-only] [--] [<version>]

Not to sure what to do here, I tried to navigate around but I cannot seem to get to anywhere but /workspace when using cd, anyone have ideas about this?

Ksenia Breitenmoser
Ksenia Breitenmoser
20,874 Points

Guys! when I send email I am redirected to index.php, but there is no information about my email (subject etc.) instead written "Logs are empty" just like as if I never sent email. Checked everything. Compared with Alena's code. Please have a look at my composer.json.

{
    "require": {
        "phpmailer/phpmailer": "^5.2.2",
        "league/oauth2-google": "^2.0"

    }
}

When running composer show: guzzlehttp/guzzle 6.3.3 Guzzle is a PHP HTTP client library
guzzlehttp/promises v1.3.1 Guzzle promises library
guzzlehttp/psr7 1.4.2 PSR-7 message implementation that al...
league/oauth2-client 2.3.0 OAuth 2.0 Client Library
league/oauth2-google 2.2.0 Google OAuth 2.0 Client Provider for...
paragonie/random_compat v2.0.17 PHP 5.x polyfill for random_bytes() ...
phpmailer/phpmailer v5.2.26 PHPMailer is a full-featured email c...
psr/http-message 1.0.1 Common interface for HTTP messages

Would very much appreciate some help. Thanks a lot

Are you running this locally? I ran into the same issue and found out it's because the logs/ folder where the email is saved isn't in the Zip file. I created that folder and the emails started appearing.

Yohan Park
Yohan Park
7,148 Points

Did the

$mail = new PHPMailer\PHPMailer\PHPMailer();

Getting error

"Mailer Error: You must provide at least one recipient email address."

So, not recognizing email.php line 47??

$mail->addAddress('treehouse@localhost', 'John Doe');

Changing it to .com will then accept the form submit but it will not log the email.

Receiving this error

PHP Notice Undefined index: REQUEST_METHOD in /home/treehouse/workspace/email.php on line 5

To the best of my knowledge, all vendor files are properly set up. I believe the issue is that I am using version 6.0 whereas the tutorial illustrates the use of version 5.2. Not sure which direction to go in!

The entire project can be viewed here.

Thanks in advance for your assistance!

Hey MM - Are you running this from the command line? If so, I don't think the $_SERVER array would be populated. You could add an isset check to ensure that the offset exists then check the REQUEST_METHOD is equal POST.

Hope that helps.

Cheers,

Ben

UPDATE: @benpayne0925 After reviewing the composer.json file, I realized that I had not actually included phpmailer in the project.

Now the issue is that the mail appears to process, but no logs are created/populated.

Hey Jonathan,

Without code it's hard to tell for sure. But the first step is to make sure you are requiring the vendor/autoload.php file.

<?php

require_once __DIR__ . '/vendor/autoload.php'; // the path from the current directory

Hopefully that helps.

-Ben