$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'email@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('email@gmail.com', 'name');
$mail->addAddress('email@gmail.com', 'Joe User');
//Content
$mail->isHTML(true);
$mail->Subject = "A subject";
$mail->Body = "The body";
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
With the above code I get the following error
2020-09-04 04:59:49 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP t24sm4345848pgo.51 - gsmtp
2020-09-04 04:59:49 CLIENT -> SERVER: EHLO localhost
2020-09-04 04:59:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [27.34.104.219]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2020-09-04 04:59:50 CLIENT -> SERVER: STARTTLS
2020-09-04 04:59:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS SMTP Error: Could not connect to SMTP host.
2020-09-04 04:59:50 CLIENT -> SERVER: QUIT
2020-09-04 04:59:50
2020-09-04 04:59:50 SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
Please help