Fatal error: class "PHPMailer" not found

  • I tried: include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');

Fatal error: class "PHPMailer" not found in C: \ Inetpub \ wwwroot \ php \ index.php on line 151

I put PHPMailerAutoload.php in the same directory as my script.

Can someone help me?

+8
php phpmailer
source share
6 answers

It does not look like all the files needed to use this class are present. I would start from the beginning:

  • Download the package from https://github.com/PHPMailer/PHPMailer by clicking the "Download ZIP" button in the lower right corner of the page.
  • extract zip file
  • upload the language folder class.phpmailer.php, class.pop3.php, class.smtp.php and PHPMailerAutoload.php all into the same directory on your server, I like to create a directory on the phpmailer server to host it all.
  • Include the class in your PHP project: require_once('phpmailer/PHPMailerAutoload.php');
+10
source share

It is just a namespace. Look at the examples for reference - you need to either use a class with names or refer to it, for example:

 use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; //Load composer autoloader require 'vendor/autoload.php'; 
+4
source share

all answers are out of date. Most of the current version (as of February 2018) no longer has autoload, and PHPMailer should be initialized as follows:

 <?php require("/home/site/libs/PHPMailer-master/src/PHPMailer.php"); require("/home/site/libs/PHPMailer-master/src/SMTP.php"); $mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only $mail->SMTPAuth = true; // authentication enabled $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail $mail->Host = "smtp.gmail.com"; $mail->Port = 465; // or 587 $mail->IsHTML(true); $mail->Username = "xxxxxx"; $mail->Password = "xxxx"; $mail->SetFrom("xxxxxx@xxxxx.com"); $mail->Subject = "Test"; $mail->Body = "hello"; $mail->AddAddress("xxxxxx@xxxxx.com"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; } ?> 
+3
source share

I suggest you take a look at getting composer . https://getcomposer.org Composer simplifies the creation of third-party libraries and uses one autoloader for all. It also standardizes where all your dependencies are, as well as some automation features.

Download https://getcomposer.org/composer.phar to C:\Inetpub\wwwroot\php

Delete the directory C:\Inetpub\wwwroot\php\PHPMailer\ .

Use composer.phar to get phpmailer package using command line to execute

 cd C:\Inetpub\wwwroot\php php composer.phar require phpmailer/phpmailer 

Upon completion, it will create the C:\Inetpub\wwwroot\php\vendor directory along with all the phpmailer files and create an autoloader.

Next, in your main project configuration file, you need to include the startup file.

require_once 'C:\Inetpub\wwwroot\php\vendor\autoload.php';

vendor\autoload.php will contain information for using $mail = new \PHPMailer;

Further information on the PHPMailer package can be found at https://packagist.org/packages/phpmailer/phpmailer

+2
source share

Just by reading what you wrote, you also need to add the class.phpmailer.php file to your directory.

0
source share

PHPMailerAutoload must be in the same folder as class.phpmailer.php

This is the PHPMailerAutoload code, which I assume:

  $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php'; 
0
source share

All Articles