PHPMailer Could not connect to SMTP host

1

I'm trying to send an email through the PHPMailer library, but I did not succeed: /.

Server: HostGator

Following code:

<?php 
$nome = $_POST["nome"];
$email = $_POST["email"];
$assunto = $_POST["assunto"];
$msg  = $_POST["msg"];

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require '../phpmailer/vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing 'true' enables exceptions

try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.hostgator.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'minhaSenha';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
    $mail->Port = 25;                                    // TCP port to connect to

    //Define o remetente
    $mail->setFrom($email, $nome);

    //Define o destinatário
    $mail->addAddress('[email protected]');     // Add a recipient

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $assunto;
    $mail->Body    = '<html>De: '.$nome.'<br/>Email:'.$email.'<br/>Assunto:'.$assunto.'Mensagem: '.$msg.'</html>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

Error message:

  

2018-01-12 19:18:00 SERVER - > CLIENT: 220-br802.hostgator.com.br ESMTP   Exim 4.89 # 1 Fri, 12 Jan 2018 17:18:00 -0200 220-We do not authorize   the use of this system to transport unsolicited, 220 and / or bulk   email.

     

2018-01-12 19:18:00 CLIENT - > SERVER: EHLO zulpix.com

     

2018-01-12 19:18:00 SERVER - > CLIENT: 250-br802.hostgator.com.br Hello   zulpix.com [50.116.87.189] 250-SIZE   52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250   HELP

     

2018-01-12 19:18:00 CLIENT - > SERVER: STARTTLS

     

2018-01-12 19:18:00 SERVER - > CLIENT: 220 TLS go ahead SMTP Error:   Could not connect to SMTP host.

     

2018-01-12 19:18:00 CLIENT - > SERVER: QUIT

     

2018-01-12 19:18:00 SERVER - > CLIENT: 221 br802.hostgator.com   closing connection SMTP Error: Could not connect to SMTP host. Message   could not be sent.Mailer Error: SMTP Could not connect to SMTP   host.

I was able to make it work in gmail, using the necessary settings for it. And I also had to enable the "Allow less secure apps" option in gmail.

But I can not send it to hostgator's email.

Could anyone help me?

OBS: We do not authorize the use of this system to transport unsolicited, 220 and / or bulk email. p>

Would this block their server?

    
asked by anonymous 12.01.2018 / 20:32

1 answer

2

Port 25 is not a secure port, nor is it used in TLS:

$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
$mail->Port = 25;                                    // TCP port to connect to

And according to this link link

The correct ports are:

  • IMAP SSL - 993
  • SMTP SSL - 465

In other words, SMTP uses SSL and not TLS with port 465, correct:

$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
    
12.01.2018 / 21:49