Home > Backend Development > PHP Tutorial > 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
Release: 2024-11-15 16:08:03
Original
714 people have browsed it

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();
Copy after login
  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]");
Copy after login
  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";
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template