'phpMailer doesn't send mail gives error on my error log, codes below

I have been trying to get phpmailer to send mail, but it keeps returning error on my errorlog.php Keeps echoing the error message, yet I have tried everything I could to no avail. Anyone with any idea or have faced similar problems can help out, please. I'm seriously stuck here. Thank you!

session_start();

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

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

require_once 'auth.php';

$user = new Auth();

//Handle Register Ajax Request
if (isset($_POST['action']) && $_POST['action'] == 'register') {
    $name = $user->test_input($_POST['name']);
    $email = $user->test_input($_POST['email']);
    $pass = $user->test_input($_POST['password']);
    $address = $user->test_input($_POST['address']);
    $referrer = $user->test_input($_POST['referrer']);
    $password = password_hash($pass, PASSWORD_DEFAULT);

    if ($user->user_exist($email)) {
        echo $user->showMessage('warning', 'This E-Mail is already registred.');
    } else {
        if ($user->register($name,$email,$password,$address,$referrer)) {  //post to database
            echo 'register';
            $_SESSION['user'] = $email;

            $token = uniqid();
            $token = str_shuffle($token);
            $user->forgot_password($token,$email);

            $mail->isSMTP();
            $mail->Host = 'smtp.gmail.com';
            $mail->SMTPAuth = true;
            $mail->Username = Database::USERNAME;
            $mail->Password = Database::PASSWORD;
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $mail->Port = 587;

            $mail->setFrom(Database::USERNAME,'[email protected]');
            $mail->addAddress($email);

            $mail->isHTML(true);
            $mail->Subject = 'title';
            $mail->Body = 'Message';
            
            $mail->send();

        } else {
            echo $user->showMessage('danger', 'Something went wrong. Try again later.');
        }
    }
}

auth.php requires config.php where the USERNAME and PASSWORD are both defined

<?php

class Database {

    const USERNAME = '[email protected]';
    const PASSWORD = 'password';

below is the latest error on my log

[26-Feb-2022 13:10:12 UTC] PHP Fatal error:  Uncaught 
PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in 
.../assets/php/vendor/phpmailer/phpmailer/src/PHPMailer.php:2035
Stack trace:
#0 .../phpmailer/phpmailer/src/PHPMailer.php(1855): 
PHPMailer\PHPMailer\PHPMailer->smtpConnect()
#1 ...phpmailer/phpmailer/src/PHPMailer.php(1598): 
PHPMailer\PHPMailer\PHPMailer->smtpSend()
#2 .../phpmailer/phpmailer/src/PHPMailer.php(1434): 
PHPMailer\PHPMailer\PHPMailer->postSend()
#3 .../assets/php/action.php(52): PHPMailer\PHPMailer\PHPMailer->send()
#4 {main}
  thrown in .../phpmailer/phpmailer/src/PHPMailer.php on line 2035


Sources

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

Source: Stack Overflow

Solution Source