Home > Article > Backend Development > Why am I getting the 'Fatal error: Class 'PHPMailer' not found' error in PHP?
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:
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"; }
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!