title: Sending an email from WordPress through REST API with an Axios call
body: <p>I'm using <a href="https://gist.github.com/nandomoreirame/5966bc4b12588904c214277328ba701a" rel="nofollow noreferrer">this snippet</a> to send an email from WordPress through the REST API.</p>
  
  <p>My <code>Vue</code>-based front end is posting form data to WordPress using an Axios <code>put()</code> call:</p>
  
  <pre><code>sendEmail () {
    const formData = new FormData()
    formData.append('contact_name', this.contactName)
    formData.append('contact_email', this.contactEmail)
    formData.append('contact_message', this.contactMessage)
    this.$axios.$put(`${this.baseUrl}/wp-json/contact/v1/send`, formData)
      .then((res) =&gt; {
        this.success = true
      })
      .catch((err) =&gt; {
        this.$toast.error(err.response)
      })
  }
  </code></pre>
  
  <p>I suppose the code above is correct, and that the issue lies on the WordPress side:</p>
  
  <p>Functions.php:</p>
  
  <pre><code>function sendContactMail(WP_REST_Request $request) {
    $response = array(
      'status' =&gt; 304,
      'message' =&gt; 'There was an error sending the form.'
    );
    $parameters = $request-&gt;get_json_params();
    if (count($_POST) &gt; 0) {
      $parameters = $_POST;
    }
    $siteName = wp_strip_all_tags(trim(get_option('blogname')));
    $contactName = wp_strip_all_tags(trim($parameters['contact_name']));
    $contactEmail = wp_strip_all_tags(trim($parameters['contact_email']));
    $contactMessage = wp_strip_all_tags(trim($parameters['contact_message']));    
    if (!empty($contactName) &amp;&amp; !empty($contactEmail) &amp;&amp; !empty($contactMessage)) {
      $subject = "(New message sent from site $siteName) $contactName &lt;$contactEmail&gt;";
      $body = "&lt;h3&gt;$subject&lt;/h3&gt;&lt;br/&gt;";
      $body .= "&lt;p&gt;&lt;b&gt;Name:&lt;/b&gt; $contactName&lt;/p&gt;";
      $body .= "&lt;p&gt;&lt;b&gt;Email:&lt;/b&gt; $contactEmail&lt;/p&gt;";
      $body .= "&lt;p&gt;&lt;b&gt;Message:&lt;/b&gt; $contactMessage&lt;/p&gt;";
      if (send_email($contactEmail, $contactName, $body)) {
        $response['status'] = 200;
        $response['message'] = 'Form sent successfully.';
      }
    }
    return json_decode(json_encode($response));
    exit();
  }
  
  add_action('rest_api_init', function () {
    register_rest_route( 'contact/v1', '/send', array(
      'methods' =&gt; 'POST',
      'callback' =&gt; 'sendContactMail'
    ));
  });
  </code></pre>
  
  <p>However, I have no idea how to troubleshoot the issue, because whichever problem is occuring here doesn't produce any input that I can see (I don't have access to server PHP log unfortunately).</p>
  
  <p>Any idea about what's wrong in my code or how I could troubleshoot it?</p>
  
  <p>Just in case below is the code of the <code>send_email()</code> function as well:</p>
  
  <pre><code>function send_email($form_email, $form_name, $form_message) {
    $email_subject = 'Message from '. get_bloginfo('name') . ' - ' . $form_email;
    $headers = "From: '" . $form_name . "' &lt;" . $form_email . "&gt; \r\n";
    $headers .= "Reply-To: ". strip_tags($form_email) . "\r\n";
    $headers .= "Content-Type:text/html;charset=utf-8";
    $email_message = '&lt;html&gt;&lt;body&gt;';
    $email_message .= "&lt;table&gt;";
    $email_message .= "&lt;tr&gt;&lt;td&gt;NAME: &lt;/td&gt;&lt;td&gt;" . $form_name . "&lt;/td&gt;&lt;/tr&gt;";
    $email_message .= "&lt;tr&gt;&lt;td&gt;MESSAGE: &lt;/td&gt;&lt;td&gt;" . $form_message . "&lt;/td&gt;&lt;/tr&gt;";
    $email_message .= "&lt;/table&gt;";
    $email_message .= "&lt;/body&gt;&lt;/html&gt;";
    $email_message = nl2br($email_message);
    wp_mail('me@gmail.com', $email_subject, $email_message, $headers);
  }
  </code></pre>