Help with PHPMailerAutoload.php


I am trying to send an email using the php library that is PHPMailerAutoload.php The reason I use this library is to retrieve a user's password. NOTE: I am using gmail. I have the code configured this way:

//Método getValor
function getValor($campo, $campoWhere, $valor) {
$ConexionBD = con();

$stmt = $ConexionBD->prepare("SELECT $campo FROM cliente_correo WHERE $campoWhere = ? LIMIT 1");
$stmt->bind_param('s', $valor);
$num = $stmt->num_rows;

if ($num > 0) {
    return $_campo;
else {
    return null;    

function enviarEmail($email, $nombre, $asunto, $cuerpo){
require 'php/PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = '';
$mail->Port = 587;

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

$mail->setFrom('[email protected]'); //REMITENTE 
$mail->addAddress($email, $nombre); //DESTINATARIO

$mail->Subject = $asunto;
$mail->Body    = $cuerpo;

if($mail->send() == false) {
    //return true;
    echo "No se pudo enviar ";
    echo "Error de PHPMailer  ".' '.$mail->ErrorInfo;
else {
    //return false;
    echo "El correo se envió ;)";


My HTML form:

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>" role="form" class=" form-horizontal login-form">        
                <div class="login-wrap">
                    <p class="login-img"><i class="icon_lock_alt"></i></p>
                    <div class="input-group">
                        <span class="input-group-addon"><i class="icon_profile"></i></span>
                        <input type="email" name="recover_mail" class="form-control" 
                               placeholder="Introduce tu correo electrónico" required >
                    <button class="btn btn-primary btn-lg btn-block" type="submit" name="enviar_email">Enviar</button>
                <div class="panel-footer">
                    ¿No te has registrado? <a href="cliente_registrar.php">Registrate ahora</a>

The POST method I have it like this:

if(!empty($_POST)) {
$email = mysqli_real_escape_string($ConexionBD, $_POST['recover_mail']);

if(!isEmail($email)) {
    $errors[] = "Debe ingresar un email valido";

if(emailExiste($email)) {
    //hace una consulta a la tabla cliente_correo
    $user_id = getValor('idCliente', 'correoElectronico', $email);
    $correoElectronico = getValor('correoElectronico', 'correoElectronico', $email);

    $url = 'http://'.$_SERVER["SERVER_NAME"].'/Monte-Carlo_v5/cliente_pass_change.php?user_id='.$user_id;

    $asunto = 'Recuperar Password';
    $cuerpo = "Hola $email:<br><br>Se ha solicitado un reinicio de contrase&ntilde;a, por favor, para procesar este paso haz clic a la siguiente direcci&oacute;,: 
        <a href='$url'>Ok, Cambiar Password!!</a> ";

    if(enviarEmail($email, $correoElectronico, $asunto, $cuerpo)) {

        echo '
            <p class="text-success">Hemos enviado un correo electrónico a la direccion $email para restablecer tu password.</p>
            <a href="cliente_login.php">Iniciar sesion</a>

    } else {
        $errors[] = "Error al enviar email";
} else {
    $errors[] = "El email no existe";


And this is the error that I have

asked by Armando Bolaños 05.09.2017 в 00:12

1 answer


The error means that PHPMailer can not contact the server SMTP that you specified in the Host property.

You can check the SMTP port (Terminal):

telnet 587 

If the port 587 does not work you can try with 25 or 465 and use what works for you, it should be mentioned that the port 25 does not always support encryption.

In the following link it leaves possible solutions to the problem: Troubleshooting PHPMailer Problems

answered by 05.09.2017 / 07:20