Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Class 'PHPMailer' not found

Error: Fatal error: Uncaught Error: Class 'PHPMailer' not found in C:\xampp\htdocs\php-mailer\index.php:4 Stack trace: #0 {main} thrown in C:\xampp\htdocs\php-mailer\index.php on line 4

My PHP Code here:

require("src/PHPMailer.php");
require("src/Exception.php");
$mail = new PHPMailer();  

$mail->IsSMTP();                                      
$mail->Host = 'smtp.gmail.com'; 
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "gmail id";  // SMTP username
$mail->Password = "mypassword"; // SMTP password

$mail->From = "[email protected]";
$mail->FromName = "Mailer";
$mail->AddAddress("[email protected]", "Josh Adams");
$mail->AddAddress("sp");                  // name is optional
//$mail->AddReplyTo("[email protected]", "Information");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body in bold!";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
   echo "Message could not be sent. 
";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";

Please let me know what mistake I made here in my code.

like image 904
Sivaprakash D Avatar asked Dec 11 '22 06:12

Sivaprakash D


1 Answers

It's because you've not considered PHPMailer's namespace. Do one of these two things:

Change your instantiation to use a fully-qualified class name (FQCN):

$mail = new PHPMailer\PHPMailer\PHPMailer();

Alternatively, define the import at the top of your file, before you load the classes:

use PHPMailer\PHPMailer\PHPMailer;

This will allow your existing new PHPMailer line to work.

All the examples provided with PHPMailer use the latter approach, and it's also described in the troubleshooting guide.

like image 113
Synchro Avatar answered Dec 28 '22 23:12

Synchro