4

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 = "opensourcesivaprakash@gmail.com";
$mail->FromName = "Mailer";
$mail->AddAddress("siva.sing.sivan@gmail.com", "Josh Adams");
$mail->AddAddress("sp");                  // name is optional
//$mail->AddReplyTo("opensourcesivaprakash@gmail.com", "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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
Sivaprakash D
  • 318
  • 1
  • 4
  • 17
  • I don't think anyone can tell you anything more than your error is. **Class 'PHPMailer' not found ... on line 4**. It's failing at the start of the code. It can't find `PHPMailer` – Lex Jul 11 '18 at 06:03
  • Thanks for your Answer Lex, after changed my code here is my error which states "Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" completed modified my code now. – Sivaprakash D Jul 11 '18 at 06:08
  • 1
    Possible duplicate of [Fatal error: Class 'PHPMailer' not found](https://stackoverflow.com/questions/28906487/fatal-error-class-phpmailer-not-found) – mario Jul 06 '19 at 19:43

2 Answers2

12

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Yes I have updated my code Even though getting below error ** Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting ** Also, I have used the same code in Goddy server but still facing the same issue – Sivaprakash D Jul 11 '18 at 07:15
  • 2
    Why? Why did they do it? PHPMailer() would be too simple? – Martin Zvarík Nov 22 '18 at 15:05
  • The FQCN did the trick on my environment : Plesk Obsidian, PHP 7.3, CRON task launched with php CLI. Thanks – ZalemCitizen Jul 17 '20 at 08:23
1

copy the files to src\ folder, and use the following in your code:

require "src\Exception.php";
require "src\PHPMailer.php";
require "src\SMTP.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

try { 
    //your code
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Azmeer
  • 734
  • 6
  • 15