Having problem with sending email with PHPMailer?

  gmail, php, phpmailer, smtp

When I run my php program I get this error:

Undefined variable: mail in C:xampphtdocsTrying_login_registercontrollersemailcontroller.php on line 22

here is my php code:

require 'vendor/autoload.php';
require 'config/constant_email.php';

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;

$mail = new PHPMailer(true);
global $mail;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = EMAILaddress; //paste one generated by Mailtrap
$mail->Password = PASSWORD; //paste one generated by Mailtrap
$mail->Port = 587;
function sendVerificationEmail($userEmail, $token)
    $body='<h1>Send HTML Email using SMTP in PHP</h1>
    <p>This is a test email I’m sending using SMTP mail server with PHPMailer.</p>';

    $mail->Subject = 'Verify your email address';
    $mail->Body = $body;
    if ($mail->send()) {
        echo 'message has been successfully sent';

    else {
        echo 'Mailor error: ' . $mail->ErrorInfo;

I can’t find the problem. Please help me.

Source: Ask PHP