title: How to send email using contact form to different address depending on selection
body: <p>I am working on a bootstrap web page. I need to be able to send a contact form message to an email address depending on the selection made in a dropdown. For example, if they choose to contact Church, then the email needs to go to church@emailaddress.com, if they choose School, then it goes to school@emailaddress.com and if they choose Preschool, then it goes to preschool@emailaddress.com. I have no experience with PHP and very little JS.</p>
  
  <p>Below is my code...</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div class="row"&gt;
          &lt;div class="col-md-12"&gt;
              &lt;form name="sentMessage" id="contactForm" novalidate&gt;
                  &lt;div class="row control-group"&gt;
                      &lt;div class="form-group col-xs-12 floating-label-form-group controls"&gt;
                          &lt;label&gt;Name&lt;/label&gt;
                          &lt;input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name."&gt;
                          &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
                      &lt;/div&gt;
                   &lt;/div&gt;
                   &lt;div class="row control-group"&gt;
                      &lt;div class="form-group col-xs-12 floating-label-form-group controls"&gt;
                          &lt;label&gt;Email Address&lt;/label&gt;
                          &lt;input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address."&gt;
                          &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row control-group"&gt;
                      &lt;div class="form-group col-xs-12 floating-label-form-group controls"&gt;
                          &lt;label&gt;Phone Number&lt;/label&gt;
                          &lt;input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number."&gt;
                          &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row control-group"&gt;
                      &lt;div class="form-group col-xs-12 floating-label-form-group controls"&gt;
                          &lt;label&gt;Who do you wish to contact?&lt;/label&gt;
                          &lt;select class="form-control empty" id="selectDept" name="deptid"&gt;
                              &lt;option&gt;SELECT...&lt;/option&gt;
                              &lt;option&gt;Faith Lutheran Church&lt;/option&gt;
                              &lt;option&gt;K-5 School&lt;/option&gt;
                              &lt;option&gt;Touching Hearts Preschool&lt;/option&gt;
                          &lt;/select&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row control-group"&gt;
                      &lt;div class="form-group col-xs-12 floating-label-form-group controls"&gt;
                          &lt;label&gt;Message&lt;/label&gt;
                          &lt;textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."&gt;&lt;/textarea&gt;
                          &lt;p class="help-block text-danger"&gt;&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;br&gt; 
  
                  &lt;img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" /&gt;
                  &lt;input type="text" name="captcha_code" size="10" id="captcha_code" maxlength="6" /&gt;
                  &lt;a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false"&gt;&lt;span class="captchaImage"&gt;&lt;i class="fa fa-refresh"&gt;&lt;/i&gt;&lt;/span&gt;&lt;/a&gt;
  
                  &lt;br&gt;
                  &lt;br&gt;
                  &lt;div id="success"&gt;&lt;/div&gt;
                     &lt;div class="row"&gt;
                          &lt;div class="form-group col-xs-12"&gt;
                              &lt;button type="submit" class="btn btn-success btn-lg"&gt;Send&lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  session_start();
  $response = array('error' =&gt; true, 'message' =&gt; 'OK');
  
  // Check for empty fields
  if(empty($_POST['name'])        ||
  empty($_POST['email'])      ||
  empty($_POST['phone'])      ||
  empty($_POST['message'])    ||
  !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
  {
      $response['message'] = "No arguments Provided!";
      die(json_encode($response));
  }
  
  include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
  $securimage = new Securimage();
  
  if ($securimage-&gt;check($_POST['captcha_code']) == false) {
    // the code was incorrect
      $response['message'] = "The security code entered was incorrect.";
      die(json_encode($response));
  }
  
  $name = $_POST['name'];
  $email_address = $_POST['email'];
  $phone = $_POST['phone'];
  $message = $_POST['message'];
  
  // Create the email and send the message
  $to = 'church@emailaddress.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
  $email_subject = "Question from Website:  $name";
  $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
  $headers = "From: noreply@emailaddress.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
  $headers .= "Reply-To: $email_address"; 
  mail($to,$email_subject,$email_body,$headers);
  
  $response['error'] = false;
  
  die(json_encode($response));
  
  ?&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>$(function() {
  
      $("input,textarea").jqBootstrapValidation({
          preventSubmit: true,
          submitError: function($form, event, errors) {
              // additional error messages or events
          },
          submitSuccess: function($form, event) {
              // Prevent spam click and default submit behaviour
              $("#btnSubmit").attr("disabled", true);
              event.preventDefault();
  
              // get values from FORM
              var name = $("input#name").val();
              var email = $("input#email").val();
              var phone = $("input#phone").val();
              var captcha_code = $("input#captcha_code").val();
              var message = $("textarea#message").val();
              var firstName = name; // For Success/Failure Message
              // Check for white space in name for Success/Fail message
              if (firstName.indexOf(' ') &gt;= 0) {
                  firstName = name.split(' ').slice(0, -1).join(' ');
              }
              $.ajax({
                  url: "././mail/contact_me.php",
                  type: "POST",
                  dataType: 'json',
                  data: {
                      name: name,
                      phone: phone,
                      email: email,
                      captcha_code: captcha_code,
                      message: message
                  },
                  cache: false,
                  success: function(response) {
                      if (response.error) {
  
                      $('#success').html("&lt;div class='alert alert-danger'&gt;");
                      $('#success &gt; .alert-danger').html("&lt;button type='button' class='close' data-dismiss='alert' aria-hidden='true'&gt;&amp;times;")
                          .append("&lt;/button&gt;");
                      $('#success &gt; .alert-danger').append("&lt;strong&gt;The security code enstered was incorrect.");
                      $('#success &gt; .alert-danger').append('&lt;/div&gt;');
                      } else {
                      // Enable button &amp; show success message
                      $("#btnSubmit").attr("disabled", false);
                      $('#success').html("&lt;div class='alert alert-success'&gt;");
                      $('#success &gt; .alert-success').html("&lt;button type='button' class='close' data-dismiss='alert' aria-hidden='true'&gt;&amp;times;")
                          .append("&lt;/button&gt;");
                      $('#success &gt; .alert-success')
                          .append("&lt;strong&gt;Your message has been sent. &lt;/strong&gt;");
                      $('#success &gt; .alert-success')
                          .append('&lt;/div&gt;');
  
                      //clear all fields
                      $('#contactForm').trigger("reset");
                          }
                  },
                  error: function() {
                      // Fail message
                      $('#success').html("&lt;div class='alert alert-danger'&gt;");
                      $('#success &gt; .alert-danger').html("&lt;button type='button' class='close' data-dismiss='alert' aria-hidden='true'&gt;&amp;times;")
                          .append("&lt;/button&gt;");
                      $('#success &gt; .alert-danger').append("&lt;strong&gt;Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                      $('#success &gt; .alert-danger').append('&lt;/div&gt;');
                      //clear all fields
                      $('#contactForm').trigger("reset");
                  },
              })
          },
          filter: function() {
              return $(this).is(":visible");
          },
      });
  
      $("a[data-toggle=\"tab\"]").click(function(e) {
          e.preventDefault();
          $(this).tab("show");
      });
  });
  
  // When clicking on Full hide fail/success boxes
  $('#name').focus(function() {
      $('#success').html('');
  });
  </code></pre>