0

I'm creating a simple contact form that should send me emails whenever someone registers. My phpmailer function won't send any emails and does not show any error in the console log. I've searched the internet for days and it seems like my code matches everyone else's, but I'm not sure why it's not working. The platform i'm using is xampp local host. I'm also quite new to php. Please help :)

EDIT: I fixed the problem. I used ajax for my post method and realized that my isset submit statement was useless. Removing that fixed the problem!

Here's my code:

<?php
    use PHPMailer\PHPMailer\PHPMailer;
  require_once('phpMailer/Exception.php');
   require_once('phpMailer/PHPMailer.php');
    require_once('phpMailer/SMTP.php');

  $name = $_POST['name'];
  $email = $_POST['email'];
  $phone = $_POST['phoneNum'];
  $agent = $_POST['xxx'];
  $day = $_POST['appointment-day'];
  $time = $_POST['time'];


if(isset($_POST['submit'])) {
  if(isset($_POST['email']) && $_POST['email'] != '') {
   if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

     $mail = new PHPMailer();
     $mail->SMTPDebug = 1;
     $mail->Host='smtp.gmail.com';
     $mail->Port=587;
    $mail->SMTPSecure='tls';
    $mail->SMTPAuth=true;
    $mail->Username='xxx@gmail.com';
    $mail->Password='xxx';

     $mail->setFrom('xxxx@gmail.com');
     $mail->addAddress('myemail@gmail.com');
     $mail->addReplyTo($_POST['email'],$_POST['name']);

     $mail->isHTML(true);
     $mail->Subject = "Registration Form";
     $mail->Body ="Registered User's Name: ".$name."\r\n";
     $mail->Body ="Email: ".$email."\r\n";
     $mail->Body ="phone Number: ".$phone."\r\n";
     $mail->Body ="xxx? ".$agent."\r\n";
     $mail->Body ="Appointment Day and Time: ".$day." ".$time."\r\n";

   if(!$mail->Send()){
      echo "message failed";
    } else {
      echo "message sent!";
    }



   }
 }

}

?>

user14444813
  • 13
  • 1
  • 4
  • You receive `message sent` or `message failed`? – user3783243 Oct 15 '20 at 16:02
  • Nope nothing at all – user14444813 Oct 15 '20 at 16:03
  • You need to do more debugging. Either one of your `isset`s is false or you have a fatal error. You also don't need 3 `if`s use `&&` for additional parameters. – user3783243 Oct 15 '20 at 16:03
  • 1
    It doesn't show any interesting error output because you are not telling it to show any. Base your code on [the examples provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/tree/master/examples) which provide a better starting point than what you have here. Also note your `Body` will only contain the "Appointment Day..." because you're not appending to it using `.=`. – Synchro Oct 15 '20 at 16:39

0 Answers0