Why do I get error sending messages?

//Include required files PHPMailer
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';
require 'vendor/phpmailer/phpmailer/src/Exception.php';
//Define name spaces
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Create instance of PHPMailer
$mail = new PHPMailer();
//Set mailer to use smtp
//Define smtp host
$mail->Host = "smtp.gmail.com";
//Enable smtp authentication
$mail->SMTPAuth = true;
//Set the smtp encryption type (ssl/tls)
$mail->SMTPSecure = "tls";
//Port to connect smtp
$mail->Port = "587";
//Set your gmail username
$mail->Username = "username@gmail.com";
//Set gmail password
$mail->Password = "password";
//Email subject
$mail->Subject = "Test email using PHPMailer";
//Set sender email
//Enable HTML
//Email body
$mail->Body = "<h1>This is an HTML h1 Heading</h1></br><p>This is an html paragraph</p>";
//Add recipient
//Finally send the email
if ( $mail->send() ) {
 echo "Email Sent..!";
 echo "Message could not be sent. Mailer Error:" . $mail->ErrorInfo;
//Closing smtp connection

Message could not be sent. Mailer Error:SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubl...
April 4th 20 at 13:22
1 answer
April 4th 20 at 13:24
$mail->setFrom('username@gmail.com', 'username');
it did not help. All the exact same error - laury_Morar commented on April 4th 20 at 13:27

Find more questions by tags PHP