Class 'PHPMailer' Not Found: Resolving Fatal Error in PHP
Despite including the 'PHPMailerAutoload.php' file in the same directory as your script, you encounter a "Fatal error: Class 'PHPMailer' not found" error. This issue can be attributed to outdated solutions and changes in the latest PHPMailer version.
Current Solution:
To resolve this error in the latest versions of PHPMailer, you need to follow a revised initialization procedure:
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"; }
By implementing this updated initialization method, you should be able to use PHPMailer without encountering the "Fatal error: Class 'PHPMailer' not found" issue.
The above is the detailed content of Why Am I Getting a 'Fatal error: Class 'PHPMailer' not found' Error in PHP, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!