Home > Article > Backend Development > How to Fix the "Fatal Error: Class 'PHPMailer' Not Found" Issue in PHP?
Resolving the "Fatal Error: Class 'PHPMailer' Not Found" issue
When attempting to utilize the PHPMailer library, you may encounter a fatal error that indicates the class 'PHPMailer' cannot be found. This issue arises when the library is not properly included in your PHP script.
To resolve this error, you have previously attempted to include the file 'PHPMailerPHPMailerAutoload.php' using 'include_once()'. However, recent updates to the library have eliminated the autoload functionality, requiring a different method of initialization.
The following code snippet outlines the updated initialization process for PHPMailer:
<?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("[email protected]"); $mail->Subject = "Test"; $mail->Body = "hello"; $mail->AddAddress("[email protected]"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; } ?>
Ensure that you replace the paths in the require() statements and the email addresses with your own relevant information.
By following this updated initialization process, you should be able to successfully use the PHPMailer library without encountering the "Fatal Error: Class 'PHPMailer' Not Found" issue.
The above is the detailed content of How to Fix the "Fatal Error: Class 'PHPMailer' Not Found" Issue in PHP?. For more information, please follow other related articles on the PHP Chinese website!