Home  >  Article  >  Backend Development  >  Why am I getting the 'Fatal error: Class 'PHPMailer' not found' error in PHP?

Why am I getting the 'Fatal error: Class 'PHPMailer' not found' error in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-11-24 01:51:10781browse

Why am I getting the

Fatal Error: Class 'PHPMailer' Not Found

When attempting to utilize the PHPMailer library in a PHP script, you may encounter the error "Fatal error: Class 'PHPMailer' not found." This issue arises due to the inability to locate the PHPMailer class definition within your script.

To address this problem, ensure that the PHPMailerAutoload.php file is included in your script correctly. This file should be placed in the same directory as your script, and the following code should be used to include it:

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

However, note that the latest version of PHPMailer (as of February 2018) no longer uses the autoload mechanism. To initialize PHPMailer in current versions, follow these steps:

  1. Require the PHPMailer.php and SMTP.php files:
require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");
require("/home/site/libs/PHPMailer-master/src/SMTP.php");
  1. Create a new PHPMailer instance:
$mail = new PHPMailer\PHPMailer\PHPMailer();
  1. Configure PHPMailer settings (e.g., SMTP, authentication, host, port, etc.):
    $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("[email protected]");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("[email protected]");
  1. Send the email:
     if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message has been sent";
     }

The above is the detailed content of Why am I getting the 'Fatal error: Class 'PHPMailer' not found' error in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn