Home  >  Article  >  Backend Development  >  How to Fix "Fatal Error: Class 'PHPMailer' Not Found" with Updated Configurations?

How to Fix "Fatal Error: Class 'PHPMailer' Not Found" with Updated Configurations?

Linda Hamilton
Linda HamiltonOriginal
2024-11-15 16:08:03625browse

How to Fix

Resolving "Fatal Error: Class 'PHPMailer' Not Found" with Updated Configurations

The error "Fatal error: Class 'PHPMailer' not found" occurs when your code cannot locate the PHPMailer class. To resolve this, the outdated approach of using include_once() is no longer applicable. The latest version of PHPMailer requires a different initialization process.

Solution:

  1. Locate the PHPMailer Files:

    • Place the following files in the same directory as your script:

      • PHPMailer.php
      • SMTP.php
  2. Initialize the PHPMailer Class:

    • Use the following code to initialize a new PHPMailer instance:
require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");
require("/home/site/libs/PHPMailer-master/src/SMTP.php");

$mail = new PHPMailer\PHPMailer\PHPMailer();
  1. Configure SMTP Settings:

    • Replace the placeholders in the following code with your own SMTP settings:
$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:

    • Use the following code to send the email:
if (!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}

The above is the detailed content of How to Fix "Fatal Error: Class 'PHPMailer' Not Found" with Updated Configurations?. 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