Class 'PHPMailer' not found

Hello

I’m new to PHP and trying to set up an SMTP mail thing, following https://github.com/PHPMailer/PHPMailer/ but keep getting an error called “Class ‘PHPMailer’ not found”.

I’m guessing it is quite a simple fix - I think I have followed the instructions, I have been reading about similar errors for a couple of hours now and nothing fixes it. Oh I should add that it is on my Contact Me page on http://ineedaweatherforecast.co.uk/.

So at the top of my index file I have require (‘phpmailer/PHPMailerAutoload.php’);

My form later in index.php references - this has the following code (and more):

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'my e-mail';                 // SMTP username
$mail->Password = 'my password';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

phpmailer is the directory where all the required files such as class.phpmailer are stored.

Can anyone work out what I am doing wrong please?

I should add that I am using PHP 5.

Thanks
James

Do you use namespaces? If so, then try to explicitly specify root namespace for PHPMailer class:

$mail = new \PHPMailer;

Otherwise, check file location.

Thanks for your response. I don’t know what namespaces are so I assume I am not using them. I did try with your amendment but still got the same error.

As far as I can see, every file is in the right location.

The original error code is
Fatal error: Class ‘PHPMailer’ not found in /var/sites/i/ineedaweatherforecast.co.uk/public_html/phpmailer/formmail.php on line 3

Do you think it could be something to do with the “var/sites/i/” bit? I have no idea why it is putting that in?

Did you install using composer? If so then require ‘vendor/autoload.php’; Composer is really the way to go.

If not, try adding error_reporting(E_ALL); at the top of your file. Might get an error message. Verify phpmailer/PHPMailerAutoload.php actually exists.

PHPMailer is a fine component but if you just getting started you might want to use a more up to date package such as SwiftMailer. Nowadays, just about everything uses composer and namespaces.

1 Like

No I didn’t use anything called Composer though it has a file included called composer.json and one called composer.lock

I think I might go back to my simple solution that worked (http://blog.teamtreehouse.com/create-ajax-contact-form), albeit with a 30 minute delay, until I know more about PHP. I only started using it a month ago.

One quick question though, is this the Composer that you mention? If so I will follow the course and learn it that way https://www.lynda.com/Composer-tutorials/Up-Running-PHP-PEAR-PECL-Composer/119001-2.html

Thanks for your response.

James

You have to include the PHPMailerAutoload.php file.

I had require (‘phpmailer/PHPMailerAutoload.php’); - is that not sufficient? I did also try include rather than require That was right at the top of my page.

What is the entire code? Show us all of it so we know what’s going on. Don’t just show fragments of it. Show us the entire file.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.