'"use PHPMailer\PHPMailer\PHPMailer" not working

I am trying to make a form that can contact me via email with PHP. I am new to PHP so after watching a couple of videos I am making with PHPMailer. After I downloaded the zip file from GitHub and installing composer and these things, the code shows some errors. When I declare "use\PHPMailer\PHPMailer\PHPMailer," it says Unexpected, 'Unknown'.

My code is this

<?PHP
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

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

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

try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
$mail->isSMTP();                                            //Send using SMTP
$mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   //Enable SMTP authentication
$mail->Username   = '[email protected]';                     //SMTP username
$mail->Password   = 'secret';                               //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('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Joe User');     //Add a recipient
$mail->addAddress('[email protected]');               //Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

//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 = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
echo 'Message has been sent';
 } catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

The code is from the GitHub page I just copied and pasted it to test it. I edited the JSON file and all of them but it doesn't work. Appreciate the help from everyone. Thank you



Solution 1:[1]

The most likely explanation is that you are running PHP that’s older than version 5.3, which is when namespaces and the use statement were introduced. Upgrade your PHP - any new development should be using 8.0.

Solution 2:[2]

Move your autoload to the top of the file, using PSR4 before the autoload is in wont work.

<?php
require 'vendor/autoload.php';
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
...

Though you should really keep your autoloader in a seperate file. e.g. a index.php and create a Mailer class of your own.

How you would go about doing this would be adding an autoload to your composer.json

{
    "name": "acme/app",
    "type": "project",
    "authors": [
        {
            "name": "some name",
            "email": "[email protected]"
        }
    ],
    "require": {
        "phpmailer/phpmailer": "^6.4"
    },
    "autoload": {
        "psr-4": {"Acme\\": "src/"}
    }
}

Creating a src folder with a file named App.php

<?php
namespace Acme;

class App
{
    public static function boot()
    {
        echo 'works'; // Do what ever you want. 
    }
}

Create a index.php in the root

<?php
require 'vendor/autoload.php';

Acme\App::boot();

then run composer install or composer du and run php index.php and your set.

Solution 3:[3]

I have seen cases where the classes were placed inside a PHP function. That will throw an error.

From the documentation:

These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Synchro
Solution 2
Solution 3