Возникли проблемы с PHPMailer

Я пытаюсь использовать PHPMailer для отправки электронной почты Gmail. Я следил за этимсообщение

Для этого я настроил функцию, показанную ниже:

function sendEmail($email, $name) {
    $mail = new PHPMailer();
    $mail->IsSMTP(); // send via SMTP
    //IsSMTP(); // send via SMTP I commented it cos it gives an error
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = '[email protected]'; // Changed my email
    $mail->Password = "password";// Changed my password
    $mail->From = '[email protected]';
    $mail->FromName = 'FROM NAME';


    $mail->AddAddress($email);

    $mail->IsHTML(true); // send as HTML
    $mail->Subject = "Subject";
    $mail->Body = "Body";

    if (!$mail->Send()) {
        return false;
    } else {
        return true;
    }
}

К сожалению, он продолжает возвращать false. Подскажите, пожалуйста, что не так с кодом?

Редактировать: ошибка, которую я получаю, показана ниже:

SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

SMTP Error: Could not connect to SMTP host.

ОБНОВЛЕННЫЙ КОД:

 $Mail = new PHPMailer();
    $Mail->IsSMTP(); // Use SMTP
    $Mail->Host = "smtp.gmail.com"; // Sets SMTP server
    $Mail->SMTPDebug = 2; // 2 to enable SMTP debug information 
    $Mail->SMTPAuth = TRUE; // enable SMTP authentication
    $Mail->SMTPSecure = "tls"; //Secure conection
    $Mail->Port = 587; // set the SMTP port
    $Mail->Username = EMAIL; // SMTP account username
    $Mail->Password = PASS; // SMTP account password
    $Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
    $Mail->CharSet = 'UTF-8';
    $Mail->Encoding = '8bit';
    $Mail->Subject = 'SUB';
    $Mail->ContentType = 'text/html; charset=utf-8\r\n';
    $Mail->From = EMAIL;
    $Mail->FromName = 'FROM NAME';
    $Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line

    $Mail->AddAddress($email); // To:
    $Mail->isHTML(TRUE);
    $Mail->Body = "Hi";
    $Mail->AltBody = "Hi";
    $Mail->Send();
    $Mail->SmtpClose();

Ответы на вопрос(7)

Ваш ответ на вопрос