Fatal Error: Class 'PHPmailer' Not Found

Fatal error: Uncaught Error: Class 'PHPMailer' not found

Before I was using an outdated phpmailer library. Then I went to github website I searched for how to install the composer. After installing the composer, follow the steps given below:

  1. Go to your project root directory
  2. Press Shift + Right-click and then click on Window Powershell
  3. Write this command "composer require phpmailer/phpmailer". It will take 2-3 minutes to execute the command.
  4. When command executes successfully, it will create some files and folders in that particular directory. After all of this, you just have to use the following code to make your phpmailer work.

Note: Remember, use namespaces/packages at the top of your code otherwise phpmailer will not work.

<!-- Contact/Appointment Form Start-->
<?php

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

include("database.php");

if(isset($_POST['contact_submit'])) {

$i_name = $_POST['name'];
$i_phone = $_POST['phone'];
$i_email = $_POST['email'];
$i_service = $_POST['service'];
$i_subject = $_POST['subject'];
$i_message = $_POST['message'];
$i_status = true;

// Date Time Settings
date_default_timezone_set('Asia/Dubai');
//$i_date = date("d-m-Y H:i:s");
$i_date = date("d-m-Y, g:i a"); //output => 12-01-2019, 5:29 pm

// Inserting Inquiry Records in Table
$sql = "insert into inquiry_tbl(name,phone,email,service,subject,message,submission_date,status) values('$i_name','$i_phone','$i_email','$i_service','$i_subject','$i_message','$i_date','$i_status')";

if($con->query($sql)){
$last_id = $con->insert_id;
// Email Code Start

// Load Composer's autoloader
require 'vendor/autoload.php';

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.ipage.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your email'; // SMTP username
$mail->Password = 'your password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

//Recipients
$mail->setFrom('info@symbiosishomecare.com', 'New Enquiry');
$mail->addAddress('babarabid123@gmail.com', 'Babar Ali'); // Add a recipient
// $mail->addAddress('ellen@example.com'); // Name is optional
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');

// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email Subject';
$mail->Body = 'Body Content';
$mail->AltBody = 'Alternate Body content';

$mail->send();
$result='<div class="alert alert-success background-success">
<button aria-label="Close" class="close" data-dismiss="alert" type="button"><i class="fa fa-close"></i></button>Welcome <strong>' . $i_name .',</strong> Thanks For Contacting Us. We Will Get Back To You Soon.</div>';
echo $result;
echo 'Message has been sent';
}
catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

// Email Code End
}
else{
$sql_error = mysqli_error($con);
if (!empty($sql_error)) {
$result='<div class="alert alert-danger background-danger">
<button aria-label="Close" class="close" data-dismiss="alert" type="button"><i class="fa fa-close"></i></button> <strong>Error: </strong>'. $sql_error .'</div>';
echo $result;
}
else {}
}
}
else{}
mysqli_close($con);
?>

Uncaught Error: Class 'PHPMailer' not found

Method 1:

instead of this :

 //Create a new PHPMailer instance
$mail = new PHPMailer();

use this:

 //Create a new PHPMailer instance
$mail = new PHPMailer\PHPMailer\PHPMailer();

Edit on 2022-09-03

Method 2: (correct way)

as @Synchro said in comments, you can use the namespace in your script file instead of full namespace/class call:

// add namespace in top of your script
use PHPMailer\PHPMailer\PHPMailer;
// then call specify the class with this :
$mail = new PHPMailer();

Error: Class 'PHPMailer' not found

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.

Class 'PHPMailer\PHPMailer\PHPMailer' not found in

BASE_URL contain http://192.168.1.240/project/

If you feed require with a URL the whole call happens through the web server, thus you get the result of code execution rather than code itself. You need a file system path, e.g.:

require __DIR__ . '/path/to/autoload.php';

Class 'PHPMailer\\PHPMailer\\Exception' not found

  1. Try to replace require_once "plugins/PHPMailer/PHPMailer.php"; to

require_once "./plugins/PHPMailer/PHPMailer.php";


  1. Do not include a libs directly. Use composer for an autoloading

    1. Install Composer https://getcomposer.org/download/
    2. Install PHP Mailer via Composer. Type on console composer require phpmailer/phpmailer https://packagist.org/packages/phpmailer/phpmailer
    3. Include Composer's autoloader in php file. require 'vendor/autoload.php';
    4. PROFIT. You can include and use PHP Mailer and other libs very easy


Related Topics



Leave a reply



Submit