Home > Article > Backend Development > Why am I getting a "Fatal error: Class 'PHPMailer' not found" error and how can I fix it?
PHPMailer Not Found: Resolving the Class Not Found Error
When attempting to use PHPMailer, you may encounter the error message "Fatal error: Class 'PHPMailer' not found." This issue arises when the PHPMailer library is not properly included in your script.
To resolve this issue, ensure that you have the latest version of PHPMailer, as the autoload method is now deprecated. The current approach to initializing PHPMailer involves the following steps:
require("path/to/PHPMailer.php"); require("path/to/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP(); $mail->SMTPDebug = 1; $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Host = "smtp.gmail.com"; $mail->Port = 465; $mail->IsHTML(true); $mail->Username = "your-email"; $mail->Password = "your-password"; $mail->SetFrom("from@email.com"); $mail->Subject = "Subject"; $mail->Body = "Message"; $mail->AddAddress("to@email.com");
if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; }
By following these steps, you can successfully resolve the "Fatal error: Class 'PHPMailer' not found" issue and utilize PHPMailer in your scripts.
The above is the detailed content of Why am I getting a "Fatal error: Class 'PHPMailer' not found" error and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!