title: How to get AJAX response from PHPMailer
body: <p>I'm a bit of a newbie and I'm trying to code a contact form for a website that will then send an email to my personal account.</p>
  <p>I want to receive an AJAX response when calling PHPMailer, so that I can display an alert saying 'You sent the email' or 'Email failed'. However, I think that I'm not leaving enough time between executing the code and waiting for the response. As I can send emails, but I keep getting this message in the developer panel:</p>
  <p><a href="https://i.stack.imgur.com/ZwbYI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZwbYI.png" alt="enter image description here" /></a></p>
  <p>Here is my HTML:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html lang=&quot;en&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot; /&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;section&gt;
          &lt;div class=&quot;row vh-100 vw-100 justify-content-center&quot; id=&quot;contact&quot;&gt;
              &lt;div class=&quot;row w-100 buffer snow4&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;row w-100 justify-content-center mt-3 snow4&quot;&gt;
                  &lt;div&gt;
                      &lt;h3&gt;Contact&lt;/h3&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class=&quot;row justify-content-center w-100 mt-5 snow4&quot;&gt;
                  &lt;div class=&quot;contact_form&quot;&gt;
                      &lt;div class=&quot;contact-image&quot;&gt;
                          &lt;i class=&quot;fas fa-mountain&quot;&gt;&lt;/i&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;row w-100 buffer&quot;&gt;&lt;/div&gt;
                      &lt;div id=&quot;contact_alert&quot; class=&quot;alert alert-danger alert-dismissible fade show&quot; role=&quot;alert&quot; style=&quot;display: none;&quot;&gt;
                          &lt;strong&gt;Error!&lt;/strong&gt; Please fill out the form correctly.
                          &lt;button type=&quot;button&quot; class=&quot;close&quot; onclick=&quot;hideAlert();&quot; aria-label=&quot;Close&quot;&gt;
                              &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
                          &lt;/button&gt;
                      &lt;/div&gt;
                      &lt;form id=&quot;contact-form&quot; class=&quot;form-horizontal&quot; method=&quot;post&quot;&gt;
                          &lt;div class=&quot;row&quot;&gt;
                              &lt;div class=&quot;col-6&quot;&gt;
                                  &lt;div class=&quot;form-group row&quot;&gt;
                                      &lt;input type=&quot;text&quot; class=&quot;form-control m-2&quot; id=&quot;form-name&quot; name=&quot;form-name&quot; placeholder=&quot;Name&quot; required&gt;
                                  &lt;/div&gt;
                                  &lt;div class=&quot;form-group row&quot;&gt;
                                      &lt;input type=&quot;email&quot; class=&quot;form-control m-2&quot; id=&quot;form-email&quot; name=&quot;form-email&quot; placeholder=&quot;Email&quot; required&gt;
                                  &lt;/div&gt;
                                  &lt;div class=&quot;form-group row&quot;&gt;
                                      &lt;input type=&quot;tel&quot; class=&quot;form-control m-2&quot; id=&quot;form-phone&quot; name=&quot;form-phone&quot; placeholder=&quot;Mobile number&quot; required&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;col-6&quot;&gt;
                                  &lt;div class=&quot;form-group row&quot;&gt;
                                      &lt;input type=&quot;text&quot; class=&quot;form-control m-2&quot; id=&quot;form-subject&quot; name=&quot;form-subject&quot; placeholder=&quot;Subject&quot; required&gt;
                                  &lt;/div&gt;
                                  &lt;div class=&quot;form-group row&quot;&gt;
                                      &lt;textarea style=&quot;height: 110px;&quot; class=&quot;form-control m-2&quot; rows=&quot;3&quot; id=&quot;form-message&quot; name=&quot;form-message&quot; placeholder=&quot;Write something...&quot; required&gt;&lt;/textarea&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                              &lt;div class=&quot;col-12&quot;&gt;
                                  &lt;div class=&quot;form-group&quot;&gt;
                                      &lt;button class=&quot;btn btn-outline-secondary&quot; onclick=&quot;sendMail()&quot;&gt;Send Message!&lt;/button&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/form&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class=&quot;row vw-100 justify-content-between&quot; id=&quot;footer&quot;&gt;
                  &lt;div&gt;Laura Dennis &amp;#169 2021&lt;/div&gt;
                  &lt;a class=&quot;font font2&quot; data-toggle=&quot;modal&quot; data-target=&quot;#exampleModal&quot;&gt;&lt;i class=&quot;far fa-envelope&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                  &lt;a class=&quot;font&quot; href=&quot;https://github.com/Dennis-debug&quot; target=&quot;_blank&quot;&gt;&lt;i class=&quot;fab fa-github&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                  &lt;a class=&quot;font&quot; href=&quot;https://www.linkedin.com/in/lauradennis01/&quot; target=&quot;_blank&quot;&gt;&lt;i class=&quot;fab fa-linkedin&quot;&gt;&lt;/i&gt;&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/section&gt;
  
      &lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot; integrity=&quot;sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js&quot; integrity=&quot;sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://d3js.org/d3.v4.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;http://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenMax.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js&quot; integrity=&quot;sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;vendor/js/fontawesomeall.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src='main.js'&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Here is my JavaScript:</p>
  <pre><code>function isNotEmpty(caller) {
      if (caller == &quot;&quot;) {
          $('#contact_alert').css('display', 'block');
          return false;
      } else {
          return true;
      }
  }
  
  function sendMail() {
      var name = $('#form-name').val();
      var email = $('#form-email').val();
      var phone = $('#form-phone').val();
      var subject = $('#form-subject').val();
      var message = $('#form-message').val();
  
      if (isNotEmpty(name) &amp;&amp; isNotEmpty(email) &amp;&amp; isNotEmpty(phone) &amp;&amp; isNotEmpty(subject) &amp;&amp; isNotEmpty(message)) {
          $.ajax({
              type: &quot;POST&quot;,
              url: &quot;sendMail.php&quot;,
              data: {
                  name: name,
                  email: email,
                  phone: phone,
                  subject: subject,
                  message: message
              },
              success: function (data) { console.log(data); },
              dataType: &quot;json&quot;,
              contentType: &quot;application/json&quot;
          })
      }
  }
  
  </code></pre>
  <p>Here is my PHP:</p>
  <pre><code>&lt;?php
  
  $executionStartTime = microtime(true) / 1000;
  
  //Import PHPMailer classes into the global namespace
  //These must be at the top of your script, not inside a function
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\SMTP;
  use PHPMailer\PHPMailer\Exception;
  
  //Load Composer's autoloader
  require 'vendor/autoload.php';
  
  //Instantiation and passing `true` enables exceptions
  $mail = new PHPMailer(true);
  
  try {
      //Server settings
      $mail-&gt;SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
      $mail-&gt;isSMTP();                                            //Send using SMTP
      $mail-&gt;Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
      $mail-&gt;SMTPAuth   = true;                                   //Enable SMTP authentication
      $mail-&gt;Username   = MY EMAIL;      //SMTP username
      $mail-&gt;Password   = MY PASSWORD;                      //SMTP password
      $mail-&gt;SMTPSecure = 'ssl';                                  //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
      $mail-&gt;Port       = 465;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
  
      //Recipients
      $mail-&gt;setFrom(MY EMAIL, 'Mailer');
      $mail-&gt;addAddress(MY EMAIL2, 'Laura Dennis');
      //$mail-&gt;addReplyTo($_POST['email'], $_POST['name']);
  
      //Content
      $mail-&gt;isHTML(true);                                  //Set email format to HTML
      $mail-&gt;Subject = 'hello';
      $mail-&gt;Body    = 'hello';
  
      $mail-&gt;send();
  
      $output['status']['code'] = &quot;200&quot;;
      $output['status']['name'] = &quot;ok&quot;;
      $output['status']['description'] = &quot;success&quot;;
      $output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . &quot; ms&quot;;
      $output['data'] = [];
  
      header('Content-Type: application/json; charset=UTF-8');
  
      echo json_encode($output);
  
  } catch (Exception $e) {
  
      $output['status']['code'] = &quot;400&quot;;
      $output['status']['name'] = &quot;executed&quot;;
      $output['status']['description'] = &quot;query failed&quot;;  
      $output['data'] = [];
  
      header('Content-Type: application/json; charset=UTF-8');
  
      echo json_encode($output);
  }
  
  ?&gt;
  
  </code></pre>