Ajax mail form with recipients html

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

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="subject">Subject:</label>
  <input type="text" id="subject" name="subject"><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[]" multiple>
    <option value="[email protected]">John Doe</option>
    <option value="[email protected]">Jane Doe</option>
    <option value="[email protected]">Admin</option>
  </select><br><br>
  <input type="submit" value="Send">
</form>

JavaScript (using jQuery):

$(document).ready(function() {
  $('#contact-form').submit(function(event) {
    event.preventDefault();
    var formData = $(this).serialize();
    $.ajax({
      type: 'POST',
      url: 'sendmail.php',
      data: formData,
      success: function(data) {
        alert('Message sent successfully!');
      },
      error: function(xhr, status, error) {
        alert('Error sending message: ' + error);
      }
    });
  });
});

PHP (sendmail.php):

<?php
  // Configuration
  $to = array();
  $subject = $_POST['subject'];
  $message = $_POST['message'];

  // Get recipients from form
  $recipients = $_POST['recipients'];

  // Loop through recipients and add to $to array
  foreach ($recipients as $recipient) {
    $to[] = $recipient;
  }

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

  $body = 'From: '. $_POST['name']. "\r\n".
          'Email: '. $_POST['email']. "\r\n".
          'Subject: '. $subject. "\r\n".
          $message;

  foreach ($to as $recipient) {
    mail($recipient, $subject, $body, $headers);
  }

  // Output success message
  echo 'Message sent successfully!';
?>

In this example, we have a form with fields for name, email, subject, message, and recipients. The recipients field is a multiple select box with options for different email addresses.

When the form is submitted, the JavaScript code prevents the default form submission behavior and sends the form data to the sendmail.php script using AJAX.

The sendmail.php script gets the form data, loops through the recipients, and sends an email to each recipient using the mail() function. The email body includes the sender's name and email, the subject, and the message.

Note that this is just an example and you should modify the script to fit your specific needs. You may also want to add error handling and validation to ensure that the form data is valid and the email is sent successfully.