Ajax mail form with recipients html content

Here is an example of an AJAX mail form with recipients HTML content:

HTML:

<form id="contact-form">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name"><br><br>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email"><br><br>
  <label for="message">Message:</label>
  <textarea id="message" name="message"></textarea><br><br>
  <label for="recipients">Recipients:</label>
  <select id="recipients" name="recipients">
    <option value="[email protected]">Admin</option>
    <option value="[email protected]">Sales</option>
    <option value="[email protected]">Support</option>
  </select><br><br>
  <input type="submit" value="Send">
</form>

JavaScript (using jQuery):

$(document).ready(function() {
  $('#contact-form').submit(function(event) {
    event.preventDefault();
    var name = $('#name').val();
    var email = $('#email').val();
    var message = $('#message').val();
    var recipients = $('#recipients').val();
    $.ajax({
      type: 'POST',
      url: 'sendmail.php',
      data: {
        name: name,
        email: email,
        message: message,
        recipients: recipients
      },
      success: function(data) {
        if (data == 'success') {
          alert('Message sent successfully!');
        } else {
          alert('Error sending message. Please try again.');
        }
      }
    });
  });
});

PHP (sendmail.php):

<?php
  // Configuration
  $to = array();
  $to[] = '[email protected]'; // default recipient
  $to[] = $_POST['recipients']; // recipient selected by user

  // Set email headers
  $headers = 'From: '. $_POST['email']. "\r\n";
  $headers.= 'Reply-To: '. $_POST['email']. "\r\n";
  $headers.= 'MIME-Version: 1.0'. "\r\n";
  $headers.= 'Content-Type: text/plain; charset=UTF-8'. "\r\n";

  // Set email body
  $body = 'Name: '. $_POST['name']. "\r\n";
  $body.= 'Email: '. $_POST['email']. "\r\n";
  $body.= 'Message: '. $_POST['message']. "\r\n";

  // Send email
  foreach ($to as $recipient) {
    mail($recipient, 'Contact Form Submission', $body, $headers);
  }

  // Return success or error message
  if (mail($to[0], 'Contact Form Submission', $body, $headers)) {
    echo 'success';
  } else {
    echo 'error';
  }
?>

In this example, the form has a select element with options for different recipients. When the form is submitted, the JavaScript code sends an AJAX request to the sendmail.php script, which sends an email to the selected recipient(s) using the PHP mail() function. The email body includes the form data, including the recipient(s) selected by the user.

Note that this is just an example and you should adjust the code to fit your specific needs. Additionally, you should ensure that your server is configured to send emails and that the mail() function is working correctly.