How to resolve PHPMailer error?

Question:

I have this error when sending mail for password recovery. I attach the error:

Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( )
Connection: opened
SERVER -> CLIENT: 220-c2.iservidorweb.com ESMTP Exim 4.89_1
We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO mautomotriz.tics-tlapa.com    
SERVER -> CLIENT: 250-c2.iservidorweb.com Hello mautomotriz.tics-tlapa.com
SERVER -> CLIENT: 220 TLS go ahead
CLIENT: 535 Incorrect authentication data
ERROR: Password command failed: 535 Incorrect authentication data
SMTP Error: Could not authenticate
SERVER -> CLIENT: 221 c2.iservidorweb.com closing connection
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 

My php code is the following:

<?php
require_once 'connections/conexion.php';
require_once 'funciones/conf_email.php';

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

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

if(emailExiste($email)) {
    //hace una consulta a la tabla cliente_correo
    $user_id       = getValor('cliente.idCliente', 'correoElectronico', $email);
    $nombreUsuario = getValor('cliente.nombre', 'correoElectronico', $email);
    $aPaterno      = getValor('cliente.apellidoPaterno', 'correoElectronico', $email);


    $url = 'http://'.$_SERVER["SERVER_NAME"].'/cliente_change-password.php?user_id='.$user_id;

    $asunto = 'Recuperar Password';
    $cuerpo2 = "
    <table width='537' height='662' border='1'>
        <tbody>
            <tr>
            <td width='253' height='94'>Buen día ' $nombreUsuario $aPaterno '</td>
            <td width='557'>Has solicitado una nueva contraseña,<br>
                para proceder, haz clic a la siguiente dirección:
                <a href='$url' >Cambio de contraseña!</a>
            </td>
            </tr>
            <tr>
                <td colspan='2'>
                    <img src='https://armando.tics-tlapa.com/mail/slide-4.jpg' width='720px' height='auto'>
                </td>
            </tr>
        </tbody>
    </table>

    ";

    if(enviarEmail($email, $nombreUsuario, $asunto, $cuerpo2)) {
        echo "
            Hemos enviado un correo electronico a la direción
            $email para restablecer tu contraseña.
            ";
        //echo "<a href='cliente_login.php'>Iniciar sesion</a>";
        print "<script>setTimeout(7000); window.location='cliente_login.php';</script>";
        exit;
    } else {
        $errors[] = "Error al enviar email";
    }
} else {
    $errors[] = "El email no existe";
}
} // end POST
?>

Now, the content of the conf_email.php library is as follows:

//enviar email al cliente
function enviarEmail($email, $nombreUsuario, $asunto, $cuerpo){
require 'php/PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2; //para que me mostrara los errores, no envía el correo
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls'; // Modificar
$mail->Host = 'smtp.gmail.com'; // Modificar
$mail->Port = 587; // Modificar
$mail->Username = 'user_name@gmail.com'; // Modificar
$mail->Password = '*************'; // Modificar

$mail->setFrom('user_name@gmail.com'); // Modificar REMITENTE
$mail->addAddress($email, $nombreUsuario); // DESTINATARIO

$mail->Subject = $asunto;
$mail->Body    = $cuerpo;
$mail->IsHTML(true);

//ENVIO DEL EMAIL ...
if($mail->send() ) {
    return true;
}
else {
    return false;
}
}

My html form is the following:

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>" role="form" 
              class=" form-horizontal login-form">        
            <fieldset>
                <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 >
                    </div>
                    <label class="checkbox">
                        <span class="pull-right"> <a href="cliente_login.php"> Regresar al login</a></span>
                    </label>
                    <button class="btn btn-primary btn-lg btn-block" type="submit" 
                            name="enviar_email">Enviar</button>
                </div>
                <div class="panel-footer">
                    ¿No te has registrado? <a href="cliente_registrar.php">Registrate ahora</a>
                </div>
            </fieldset>
        </form>

When I click the Send button, I mark that error and also does not send the email to the client to recover their password. I already modified the line:

$mail->SMTPDebug = 2; //escribo un cero (0)

But still the mail is not sent and I get the error: Error sending email.

Does anyone have any idea of ​​a solution to this error? Or at least explain to me why the email was not sent to the client.

Answer:

If you're doing it from localhost, it probably won't work.

What you should do is upload it to the server so that it works for you or from localhost temporarily put:

$mail->isSMTP();
/*$mail->SMTPDebug = 2; para que me mostrara los errores, no envía el correo
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls'; // Modificar*/
$mail->Host = 'localhost'; // Modificar
/*$mail->Port = 587; // Modificar
$mail->Username = 'user_name@gmail.com'; // Modificar
$mail->Password = 'EJEMPLODEPASSWORD1234'; // Modificar*/

Remember that for emails to work from localhost you have to have Mercury configured in your Xammp:

https://www.youtube.com/watch?v=26yYcEVYgDU

Explanatory video to configure Mercury.

Scroll to Top