Error in PHPmailer: Configuration by Hostnet

0

    <?php

// 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/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';

$mail = new PHPMailer(true);                             
try {
    //Server settings
    $mail->SMTPDebug = 2;                               
    $mail->isSMTP();                                      
    $mail->Host = 'smtp.dominiodahostnet.com.br';              
    $mail->SMTPAuth = true;                              
    $mail->Username = 'usuario=dominiodahostnet.com.br';                 
    // SMTP username
    $mail->Password = '****';                                   
    //$mail->SMTPSecure = 'tls';                          
    $mail->Port = 587;                                    

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');   
    //$mail->addAddress('[email protected]');               l
    //$mail->addReplyTo('[email protected]', 'Information');
    //$mail->addCC('[email protected]');
    //$mail->addBCC('[email protected]');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    
    //Content
    $mail->isHTML(true);                                 
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $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;
}

Below the generated error

2017-09-20 17:58:23 SERVER -> CLIENT: 220 smtpq.f1.k8.com.br ESMTP Postfix
2017-09-20 17:58:23 CLIENT -> SERVER: EHLO www.dominiodahostnet.com.br
2017-09-20 17:58:23 SERVER -> CLIENT: 250-smtpq.f1.k8.com.br250-PIPELINING250-SIZE 33554432250-ETRN250-STARTTLS250-AUTH PLAIN LOGIN250-ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2017-09-20 17:58:23 CLIENT -> SERVER: STARTTLS
2017-09-20 17:58:23 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-09-20 17:58:23 CLIENT -> SERVER: QUIT
2017-09-20 17:58:23 SERVER -> CLIENT: 
2017-09-20 17:58:23 SMTP ERROR: QUIT command failed: 
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.

Warning that I am testing directly from the server and not from localhost

    
asked by anonymous 20.09.2017 / 20:12

3 answers

0

If you do not want to use TLS / SSL encryption To disable the STARTTLS and / or the secure connection, set PHPMailer like this:

$mail->SMTPSecure = false; $mail->SMTPAutoTLS = false;

Thank you, Guilherme Nascimento for your tip.

For weeks now I've been looking for a solution to this error in sending email through the Hostnet server. After PHPMailer was upgraded to version 6.0.1, I could no longer send email via my site form. After much searching for an answer online, I found your tip on this site (stackoverflow). As soon as I added it to my PHP code, it solved the problem!

    
10.11.2017 / 13:06
3
The 587 port is usually a port that uses SSL or TLS, so you should uncomment $mail->SMTPSecure and first test tls :

$mail->SMTPSecure = 'tls';

If there is no test ssl :

$mail->SMTPSecure = 'ssl';

If the error message is still :

  

Could not connect to SMTP host.

It's probably because the extension for secure connections is commented out in php.ini:

If it's Windows Server:

;extension=php_openssl.dll

If it's Linux / Unix:

;extension=openssl.so

Remove ; and restart Apache or Ngnix if you do not have control over the server and php.ini just by contacting their support.

If the intention is not to use TLS / SSL encryption

To disable STARTTLS and / or secure connection, set PHPMailer like this:

$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;

Extra note

This line seems wrong:

$mail->Username = 'usuario=dominiodahostnet.com.br';

I think the correct one is:

$mail->Username = '[email protected]';

Or without the domain:

$mail->Username = 'usuario';
    
20.09.2017 / 20:39
0

For those who encounter the same problem as me.

File php:

    <?php

    // 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/PHPMailer.php';
    require 'PHPMailer/SMTP.php';
    require 'PHPMailer/Exception.php';

    $mail = new PHPMailer(true);                             
    try {
        //Server settings
        $mail->SMTPDebug = 2;                               
        $mail->isSMTP();                                      
        $mail->Host = 'smtp.dominiodahostnet.com.br';              
        $mail->SMTPAuth = true;                              
        $mail->Username = 'usuario=dominiodahostnet.com.br';                 
        // SMTP username
        $mail->Password = '****';                                   
        //$mail->SMTPSecure = 'tls';                          
        $mail->Port = 587;                                    

        //Recipients
        $mail->setFrom('[email protected]', 'Mailer');
        $mail->addAddress('[email protected]', 'Joe User');   
        //$mail->addAddress('[email protected]');               l
        //$mail->addReplyTo('[email protected]', 'Information');
        //$mail->addCC('[email protected]');
        //$mail->addBCC('[email protected]');

        //Attachments
        //$mail->addAttachment('/var/tmp/file.tar.gz');         
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    
        //Content
        $mail->isHTML(true);                                 
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $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;
    }

.user.ini file

        authmail.debug = Off
         authmail.port = 587
         authmail.address = "[email protected]"
         authmail.smtp_account = "exemplo=seudominio.com"
         authmail.smtp = "smtp.seudominio.com"
         authmail.password = "Aqui-você-coloca-a-senha-do-seu-e-mail"

The .user.ini file should be generated in the hostnet dashboard copied and pasted into a notepad.

Done this just play via ftp on hosting and test

    
21.09.2017 / 00:55