Mailer Error: SMTP connect()

Asked

Viewed 665 times

0

This class was created to send product purchase information to the email box, but an error ("Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting")

<?php

class EnviarEmail extends PHPMailer {
    /**
   * inicializa a classe com os dados iniciais
   * @return void
   */
    function __construct() {

        parent::__construct();
        // defino que é SMTP        
        parent::IsSMTP();
                // se é em HTML
    parent::IsHTML(true);
                 // codificação charset padrao UTF8
        $this->CharSet = 'UTF-8';
        // modo debug 0=off 1 e 2=mostram informações do envio ou erros
        $this->SMTPDebug = 0;
        //Indica a porta do seu servidor
        $this->Port = Config::EMAIL_PORTA; 
        //smtp.dominio.com.br //seu servidor smtp
        $this->Host = Config::EMAIL_HOST; 
        //define se tem ou autenticação no SMTP
        $this->SMTPAuth = Config::EMAIL_SMTPAUTH; 
                // define dados do remetendo EMAIL, SENHA  da conta SMTP
        $this->FromName    = Config::EMAIL_NOME;
        $this->From        = Config::EMAIL_USER;
        $this->Username    = Config::EMAIL_USER;
        $this->Password    = Config::EMAIL_SENHA;

    }

    /** * Envia o email propriamente dito
   * @return void
   * $setor = setor , $destinatario=email dominio, assunto, msg
         * $reply = email que vai a resposta 
         */ 
    function Enviar($assunto, $msg, $destinatarios=array()) {

                  //seto dados da mensagem
        $this->Subject      = $assunto;
        $this->Body         = $msg;

                // email de resposta
                //  $this->AddReplyTo($reply);
                // email para receber  uma cópia
     //   $this->Addcc(Config::EMAIL_COPIA);

                 //passando um laço para pegar todos os destinatarios       
        foreach($destinatarios as $email):

                $this->AddAddress($email, $email); //PARA MIM

            endforeach;

                 //enviando o email 
            if (parent::Send()):

                $this->ClearAllRecipients();

            else:
            echo "<h4>Mailer Error: " . $this->ErrorInfo . "</h4>";

            endif;
    }    


}

inserir a descrição da imagem aqui

2 answers

1

Can be n things, I’ve had problems with SMTP connection for a while, does the following:

1- Change the value of Smtpdebug to 1 or 2 p/ see if the connection tells you exactly where the error is, reading the error stack is critical and sometimes saves us a lot of time;

2- check if EMAIL_HOST is correct, if you are using gmail stmp is this -> smtp.gmail.com, if it is not, check the correct address according to the email you are using, on port 587

3- Still speaking in gmail, if using of course, enables the option "Access to less secure app"

https://myaccount.google.com/lesssecureapps?pli=1

Another thing I noticed is that you are not passing $mail->Smtpsecure, it is important to enable TLS encryption

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;  
  • I’ve done all these procedures before gmail, but still presents the same error

0

This means that the email server parameters (Config::EMAIL_HOST and the like) do not point to a functional SMTP server. The problem does not seem to be in the code posted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.