0

Hay algun error en el codigo? Estoy intentando hacer uj formulario que envie un email cuando ha sido enviado con PHPMailer. Codigo enviar.php

 <?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $email=$_POST['email'];
    $message=$_POST['message'];


require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try{
    $mail->SMTPDebug = 2;  
    $mail->isSMTP();  
    $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = '[email protected]';                     // SMTP username
    $mail->Password   = 'mypassword';                               // SMTP password
    $mail->SMTPSecure = 'tls';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom($email,$name);
    $mail->addAddress('[email protected]');  

    // Add a recipient
    

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

  $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}else{
echo "not sent";}

El formulario y enviar.php estan en la en la misma carpeta junto a la carpeta PHPMAILER que contiene todo. He descargado el .zip de la pagina de github https://github.com/PHPMailer/PHPMailer

2
  • Este código te genera algún error?
    – BetaM
    el 25 abr. 2020 a las 14:03
  • No, pero en vez de funcionar, salta el echo "not sent"
    – user169386
    el 25 abr. 2020 a las 14:24

1 respuesta 1

0

Si te muestra el error not send,entonces no cumple la primera condición

if(isset($_POST['submit']))

Verifica que en el formulario del que estés enviando los datos tenga el parámetro method. Ej: <from id="my_form" action="send_mail.php" method="post">

O antes de la primera condición utiliza var_dump() para verificar como te llegan los datos:

var_dump($_POST);
exit();
if(isset($_POST['submit']))
2
  • Entonces, cambia ese sector de tu código a lo siguiente: ``` if(isset($_POST['email'])){ $name=$_POST['fname']; $email=$_POST['email']; $message=$_POST['message']; ``` el 25 abr. 2020 a las 20:25
  • SII, muchisismas gracias de verdad!!!! Ha funcionado al fin. Como ya he dicho gracias por tu tiempo y atención
    – user169386
    el 26 abr. 2020 a las 6:09

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.