Mailer Error: SMTP connect () [dated]
want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 months ago .
improve this questionThis class was created to send the 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;
}
}
2 answers
Can be N Things, I've had problems with SMTP connection for a while, does the following:
1-change the SMTPDebug value to 1 or 2 p / see if the connection tells you exactly where it is giving error, reading the error stack is fundamental and sometimes saves us a lot of Time;
2-check if EMAIL_HOST is correct, if you have using gmail the stmp is this -> smtp.gmail.com, if not, check the correct address according to the email you are using, on Port 587
3-still speaking of gmail, if you have Using of course, enable the option"access to less secure app"
Https://myaccount.google.com/lesssecureapps?pli=1
Another thing I noticed is that it is not passing the $mail- > SMTPSecure, it is important to enable TLS encryption
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
This means that the mail server parameters (Config:: EMAIL_HOST and the like) do not point to a working SMTP server. The problem does not seem to be in the posted code.