SMTP Error: Could not connect to SMTP host [duplicated]

0

Help I have this problem in my code, who helps me please this is the code:

function send_mail($email,$message,$subject)
{                       
    require_once('mailer/class.phpmailer.php');
    $mail = new PHPMailer();
    $mail->IsSMTP(); 
    $mail->SMTPDebug  = 0;   
    $mail->Mailer = "smtp";                  
    $mail->SMTPAuth   = true;                  
    $mail->SMTPSecure = "ssl";                 
    $mail->Host       = 'smtp.gmail.com';      
    $mail->Port       = 465;             
    $mail->AddAddress($email);
    $mail->Username="micorreo";  
    $mail->Password="micontraseña";            
    $mail->SetFrom('[email protected]','Coding Cage');
    $mail->AddReplyTo("[email protected]","Coding Cage");
    $mail->Subject    = $subject;
    $mail->MsgHTML($message);
    $mail->Send();
    if(!$mail->Send()) {
      echo "Error: " . $mail->ErrorInfo;
    } else {
      echo "Enviado!";
    }
}

It generates the following error:

  

SMTP Error: Could not connect to SMTP host.

    
asked by GeorgeOtalvaro 06.04.2018 в 16:12
source

3 answers

0

Following this accepted response , the possible reasons why you can not connect to the SMTP server can be:

  • It is necessary to use $mail->SMTPSecure = 'tls'; instead of ssl.
  • Let the port be 587
  • That the user or password is incorrect.

Additionally, I would use $mail->IsHTML(true); to specify the format in which the message travels.

    
answered by 06.04.2018 в 16:45
0

I see that the SMTP configuration is correct, the problem you have may be because the credentials you use are wrong, that the mail has not configured permissions for applications to use it to send emails or that your server does not have configured shipments of mail.

It has happened to me with XAMPP in local that does not send me the emails, by mail () or by SMTP, but when moving my application to a server that I am sure is configured it works correctly.

Greetings!

    
answered by 06.04.2018 в 17:54
0

For sending mail SMTP uses port 587 and authenticate tls

$mail->SMTPSecure = 'tls'; 
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = '587'; 
$mail->Username = '[email protected]'; 
$mail->Password = '***';
    
answered by 06.04.2018 в 20:11