PHPmailer does not work for me when sending an email (Troubleshooting)

3

Hello, I've been going around looking at information and I can not find any way for me to send this email to the desired mail, and I do not know why I get this error, I have disabled the firewall, I am making the request in postman in case I have something to see that in my opinion should not affect the postman at all.

I would appreciate someone throwing a handle on this mess I have.

Thanks in advance.

                /*Lo primero es añadir al script la clase phpmailer desde la ubicación en que esté*/
                require 'PHPMailer/class.phpmailer.php';
                require 'PHPMailer/class.smtp.php';
                require 'PHPMailer/PHPMailerAutoload.php';

                //Crear una instancia de PHPMailer
                $mail = new PHPMailer();
                //Definir que vamos a usar SMTP
                $mail->IsSMTP();
                //Esto es para activar el modo depuración. En entorno de pruebas lo mejor es 2, en producción siempre 0
                // 0 = off (producción)
                // 1 = client messages
                // 2 = client and server messages

                $mail->SMTPDebug  = 2;
                //Ahora definimos gmail como servidor que aloja nuestro SMTP
                $mail->Host       = 'smtp.gmail.com';
                //El puerto será el 587 ya que usamos encriptación TLS
                $mail->Port       = 587;
                //Definmos la seguridad como TLS
                $mail->SMTPSecure = 'tls';
                //Tenemos que usar gmail autenticados, así que esto a TRUE
                $mail->SMTPAuth   = true;
                //Definimos la cuenta que vamos a usar. Dirección completa de la misma
                $mail->Username   = "[email protected]";
                //Introducimos nuestra contraseña de gmail
                $mail->Password   = "ContraseñaDelEmailQueHePuesto";
                //Definimos el remitente (dirección y, opcionalmente, nombre)
                $mail->SetFrom('[email protected]', 'Mi nombre');
                //Esta línea es por si queréis enviar copia a alguien (dirección y, opcionalmente, nombre)
                // $mail->AddReplyTo('[email protected]','El de la réplica');
                //Y, ahora sí, definimos el destinatario (dirección y, opcionalmente, nombre)
                $mail->AddAddress('EmailAlQueQuieroEnviar', 'El Destinatario');
                //Definimos el tema del email
                $mail->Subject = 'Esto es un correo de prueba';
                //Para enviar un correo formateado en HTML lo cargamos con la siguiente función. Si no, puedes meterle directamente una cadena de texto.
                // $mail->MsgHTML(file_get_contents('correomaquetado.html'), dirname(ruta_al_archivo));
                //Y por si nos bloquean el contenido HTML (algunos correos lo hacen por seguridad) una versión alternativa en texto plano (también será válida para lectores de pantalla)
                $mail->AltBody = 'This is a plain-text message body';
                $mail->Body = 'Hello, this is my message.';
                echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n"; 
                //Enviamos el correo
                if(!$mail->Send()) {
                echo "Error: " . $mail->ErrorInfo;
                } else {

                echo "Enviado!";
                }

And my mistake in executing it is this:

SSL loaded

  

2018-03-22 23:03:52 SERVER - > CLIENT: 220 smtp.gmail.com ESMTP   p79sm7875211wmf.34 - gsmtp
2018-03-22 23:03:52 CLIENT - >   SERVER: EHLO localhost
2018-03-22 23:03:52 SERVER - > CLIENT:   250-smtp.gmail.com at your service, [84.121.33.176] 250-SIZE   35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250   SMTPUTF8
2018-03-22 23:03:52 CLIENT - > SERVER: STARTTLS
  2018-03-22 23:03:53 SERVER - > CLIENT: 220 2.0.0 Ready to start   TLS
SMTP Error: Could not connect to SMTP host.
2018-03-22   23:03:53 CLIENT - > SERVER: QUIT
2018-03-22 23:03:53
  2018-03-22 23:03:53
SMTP connect () failed.    link
Error:   SMTP connect () failed.    link

    
asked by Daniel 23.03.2018 в 00:10
source

2 answers

2

I leave the way I use to send emails by gmail

<?php
//Load composer's autoloader
require_once('PHPMailer/PHPMailerAutoload.php'); 

$mail = new PHPMailer(true); 
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$mail->SMTPDebug = 2; 
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;// TCP port to connect to
$mail->CharSet = 'UTF-8';
$mail->Username ='[email protected]'; //Email para enviar
$mail->Password = 'ContraseñaDelEmailQueHePuesto'; //Su password
//Agregar destinatario
$mail->setFrom('[email protected]', 'Mi nombre');
$mail->AddAddress('[email protected]');//A quien mandar email
$mail->SMTPKeepAlive = true;  
$mail->Mailer = "smtp"; 


    //Content
$mail->isHTML(true); // Set email format to HTML


$mail->Subject = 'PRUEBA 6';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
  echo 'Error al enviar email';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Mail enviado correctamente';
}

I hope I can help you

    
answered by 23.03.2018 в 06:16
0

remember that first you have to enable your google mail to allow access to less secure applications,

After accessing your gmail account, enter the following link

link

there distils that option.

once this is done just verify that the user and password are good, you have the code (server smpt, security protocol, port) so I see, if this does not work try to find the library of another repository.

you can try this

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;//587 or 465
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "tu contraseña";
$mail->SetFrom("[email protected]");

to see how it is: D

    
answered by 30.08.2018 в 20:01