I'm trying to send e-mails via PHP using PHPMailer, currently, i'm on localhost trying to test if everything is ok, but it doesn't connect with Gmail SMTP no matter what I do or change.
I've seen a lot of solutions and tutorials of how can I accomplish this here's what I have:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require 'assets/PHPMailer/src/Exception.php';
require 'assets/PHPMailer/src/PHPMailer.php';
require 'assets/PHPMailer/src/SMTP.php';
$mail = new PHPMailer;
// DADOS DO FORM
$name = $_POST["name"];
$email = $_POST["email"];
$msg = $_POST["message"];
$date = date('d-m-Y H:i:s');
$bodyContent = "<h2>Contato de <strong>" . $name . "</strong></h2><br /><h3>E-mail para resposta: <strong>" . $email . "</strong><h3><br /><br /><h5>" . $msg . "</h5><br /><br />Contato enviado em " . $date;
try{
echo '<script>console.log("ENTROU TRY")</script>';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'my-gmail-email@gmail.com';
$mail->Password = '***********';
$mail->CharSet = 'utf-8';
$mail->From = "my-gmail-email@gmail.com";
$mail->FromName = "Myself";
$mail->AddReplyTo($email, $name);
$mail->AddAddress("my-receiving@email.com", "PneuCar");
$mail->IsHTML(true);
$mail->Subject = 'Contato pelo site.';
$mail->Body = $bodyContent;
if(!$mail->send()) {
// IT LOGS SEND TRUE
echo '<script>console.log("SEND TRUE")</script>';
echo '<script type="text/javascript">alert("Contato enviado com sucesso! Aguarde um retorno.");</script>';
//header("location:javascript://history.go(-1)");
} else {
echo '<script>console.log("SEND FALSE")</script>';
//header("location:javascript://history.go(-1)");
echo '<script type="text/javascript">alert("'. $envio .'")</script>';
}
} catch (Exception $e) {
echo '<script>console.log("ENTROU CATCH")</script>';
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
I've tried port 465 with SSL and TLS, tried 587 with both too. I've allowed less secure apps. I'm logged in my Gmail account on Chrome. I'm serving my files with Apache. The PHPMailer files i use are downloaded directly from their Github. And after submiting my form i get this on console:
2017-11-01 12:02:09 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP n50sm292637qtc.44 - gsmtp
2017-11-01 12:02:09 CLIENT -> SERVER: EHLO localhost
2017-11-01 12:02:09 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [187.116.118.221]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-11-01 12:02:09 CLIENT -> SERVER: STARTTLS
2017-11-01 12:02:09 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-11-01 12:02:09 CLIENT -> SERVER: QUIT
2017-11-01 12:02:09
2017-11-01 12:02:09
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I've searched on Troubleshooting wiki for any solution, but haven't found anything.
Can someone help me figure out is wrong with my code?
EDIT
When i use this code to check if the connection is timing out
$checkconn = fsockopen('smtp.gmail.com', 587, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}
It return 'ok'.