title: Using Ajax in Wordpress to send mail
body: <p>I'm trying to use Ajax to send an email through a web form I created, but I'm lost. I have no idea how Ajax functions in Wordpress.</p>
  
  <p>I firstly created an action</p>
  
  <pre><code>add_action( 'wp_ajax_siteWideMessage', 'wpse_sendmail' );
  </code></pre>
  
  <p>The function that's supposed to retrieve the data and send the mail is:</p>
  
  <pre><code>function wpse_sendmail()
  {
      $for = like_escape($_POST['forwhat']);
      $email = like_escape($_POST['email']);
      $headers = 'From: '.$email ."\r\n".'Reply-To: '.$email;
      $message = like_escape($_POST['message_message']);
      $respond = like_escape($_POST['message_email']);
  
      wp_mail( "support@ontrgt.net", "(OTN) Support: ".$support, $message, $headers); 
  }
  </code></pre>
  
  <p>Finally, the js is like so:</p>
  
  <pre><code>$("#contact-send").click(function(){
      var forwhat = $("#contact-for").val();
      var name = $("#contact-name").val();
      var email = $("#contact-email").val();
  
      $.post( "&lt;?php echo esc_js( site_url() ) ?&gt;", { siteWideMessage:"null", forwhat: forwhat, name: name, email:email }, function(){
              console.log("success");
      });
  });
  </code></pre>
  
  <p>I'm not too sure what I'm missing here. Can someone help me understand Wordpress's Ajax process?</p>
  
  <hr>
  
  <p><strong><em>UPDATE</em></strong></p>
  
  <p>I updated my code to this so far:</p>
  
  <p>PHP</p>
  
  <pre><code>add_action( 'wp_ajax_siteWideMessage', 'wpse_sendmail' );
  add_action( 'wp_ajax_nopriv_siteWideMessage', 'wpse_sendmail' );
  
  function wpse_sendmail()
  {
      $for = $_POST['forwhat'];
      $email = $_POST['email'];
      $headers = 'From: '.$email ."\r\n".'Reply-To: '.$email;
      $message = $_POST['message_message'];
      $respond = $_POST['message_email'];
  
      /*if(wp_mail( "support@ontrgt.net", "(OTN) Support: ".$for, $message, $headers))
      {
          echo "WOOHOO";
      }*/
  
      if($for)
      {
          //Just to see if there is any response.
          echo "Whoohoo";
      }
  
      die();
  }
  </code></pre>
  
  <p>Js</p>
  
  <pre><code>$("#contact-send").click(function(){
      var forwhat = $("#contact-for").val();
      var name = $("#contact-name").val();
      var email = $("#contact-email").val();
  
      var data = { action:'siteWideMessage', forwhat:forwhat, name:name, email:email };
      $.post('&lt;?php echo admin_url("admin-ajax.php"); ?&gt;', data, function(response) {
          alert(response);
      });
  });
  </code></pre>
  
  <p>Wordpress is still not responding with my AJAX command. I'm using the inspect element, and I see no data being passed around.</p>