Can’t send emails through REST API

Question

I’m trying to send an email using wp_mail() through a REST custom route, but it’s failing somewhere and I don’t know how to troubleshoot it because I can’t output anything from the page where I’m making the Ajax call (I appended that call at the end just in case).

functions.php:

// send email function
function send_email($form_email, $form_name, $form_message) {
  $email_to = get_option('admin_email');
  $email_subject = 'Message from '. get_bloginfo('name') . ' - ' . $form_email;

  $headers = "From: '" . $form_name . "' <" . $form_email . "> rn";
  $headers .= "Reply-To: ". strip_tags($form_email) . "rn";
  $headers .= "Content-Type:text/html;charset=utf-8";

  $message_head_cell_color = "#eee";
  $email_message = '<html><body>';
  $email_message .= "<table rules='all' style='border-color: #aaa;' cellpadding='10'>";
  $email_message .= "<tr><td style='background: " . $message_head_cell_color . ";'><strong>NAME:</strong> </td><td>" . $form_name . "</td></tr>";
  $email_message .= "<tr><td style='background: " . $message_head_cell_color . ";'><strong>EMAIL:</strong> </td><td>" . $form_email . "</td></tr>";
  $email_message .= "<tr><td style='background: " . $message_head_cell_color . ";'><strong>MESSAGE:</strong> </td><td>" . $form_message . "</td></tr>";
  $email_message .= "</table>";
  $email_message .= "</body></html>";
  $email_message = nl2br($email_message);

  return wp_mail($email_to, $email_subject, $email_message, $headers);
}

// custom route callback function
function sendContactMail(WP_REST_Request $request) {
  $response = array(
    'status' => 304,
    'message' => 'There was an error sending the form.'
  );
  $parameters = $request->get_json_params();
  if (count($_POST) > 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) && !empty($contactEmail) && !empty($contactMessage)) {
    $subject = "(New message sent from site $siteName) $contactName <$contactEmail>";
    $body = "<h3>$subject</h3><br/>";
    $body .= "<p><b>Name:</b> $contactName</p>";
    $body .= "<p><b>Email:</b> $contactEmail</p>";
    $body .= "<p><b>Message:</b> $contactMessage</p>";

    if (send_email($contactEmail, $contactName, $body)) {
      $response['status'] = 200;
      $response['message'] = 'Form sent successfully.';
    }
  }
  return new WP_REST_Response($response);
}

// custom route declaration
add_action('rest_api_init', function () {
  register_rest_route( 'contact/v1', 'send', array(
    'methods' => ['POST','PUT'],
    'callback' => 'sendContactMail'
  ));
});

My call to the custom route:

this.$axios.$put(`${this.baseUrl}/wp-json/contact/v1/send`, formData)
  .then((res) => {
    this.success = true
    this.ApiResponse = res
  })
  .catch((err) => {
    this.$toast.error(err.response)
  })
0
drake035 4 months 2021-01-01T10:10:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse