En este ejemplo vemos como enviar un correo mediante PHPMailer con php utilizando una cuenta de correo smtp de gmail, antes de usar este código tienes que descargar la librería phpmailer desde este enlace PHPmailer e incluirla dentro del directorio web.

Importante! Para hacer uso de esta librería en gmail tienes que activar el uso de aplicaciones menos seguras desde la cuenta de correo gmail que vas a usar en la configuración de phpmailer, te dejo un enlace a guía de como hacerlo https://www.youtube.com/watch?v=MOnyExcIPVY

Ahora el código necesario para enviar un correo con PHPMailer y gmail:
<?php

date_default_timezone_set('Europe/Lisbon');

require 'PHPMailer/class.phpmailer.php';
require 'PHPMailer/class.smtp.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSendMail(); //or $mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

$mail->Timeout=60;

$mail->Helo = "dominio.com"; //Muy importante para que llegue a hotmail y otros

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "correo@envia.com";

//Password to use for SMTP authentication
$mail->Password = "*****";

//Set who the message is to be sent from
$mail->setFrom('correo@envia.com', 'Nombre');

//Set an alternative reply-to address
$mail->addReplyTo('correo@envia.com', 'Nombre');

//Set who the message is to be sent to
$mail->addAddress('correo@destino.com');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

$mail->Body = '<h1>This is a plain-text message body</h1> Lorem ipsum dolor sit amet, consectetur.';

$mail->IsHTML(true);

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}



?>
si de esta manera no funciona solo tienes que cambiar esto:
$mail->isSendMail(); //or $mail->isSMTP();
por esto:
$mail->isSMTP(); //or $mail->isSendMail();
salu2
1
Puntos
5883
Visitas
0
Resp
Por alber hace 8 años
Admin
Para comentar Inicia sesión o Regístrate
Phpres 2025@ All rights reserved.
Utilizamos cookies para mejorar la navegación en el sitio. Más información Aceptar