title: Wordpress send email to selected user when custom post type is created
body: <p>I am building a wordpress plugin that adds the custom post type "bookings" to the dashboard. This post type allows the admin to create a booking form and assign it to a user (from a drop down list of users) - this info (i.e. the user selected) is stored in the wp_postmeta DB table as 'wedding_user'.</p>
  
  <p>What I want to do is email the selected user when a booking (post type) is created. </p>
  
  <p>Here is what I have so far, I know this will send an email when the custom post type is created, how do I add the selected user email address in there though?</p>
  
  <pre><code>function notify_client($post_ID)  {
  // a conditional to check that this is a new post
  if( ( $_POST['post_status'] == 'publish' ) &amp;&amp; ( $_POST['original_post_status'] != 'publish' ) ) {
  
      $user_email_address = USER EMAIL ADDRESS
  
      // create the from details 
      $headers[] = 'From: Bookings &lt;bookings@thedistractionsband.co.uk&gt;';
      // lets cc in the head just because we can 
      $headers[] = 'Cc: Joseph Malik &lt;sam.skirrow@gmail.com&gt;';
      // separate the users array
      $send_to = $user_email_address;
      // concatenate a message together
      $message = 'Test Message';
      // and finally send the email
      wp_mail($send_to, "Test Message", $message, $headers );
      return $post_ID;
      }
  }
  add_action('publish_bookings', 'notify_client');
  </code></pre>
  
  <p>Output of <code>var_dump($_POST);</code></p>
  
  <pre><code>array(59) { 
      ["_wpnonce"]=&gt; string(10) "0d5335d18f" 
      ["_wp_http_referer"]=&gt; string(60) "/distractions-login/wp-admin/post-new.php?post_type=bookings" 
      ["user_ID"]=&gt; int(1) 
      ["action"]=&gt; string(8) "editpost" 
      ["originalaction"]=&gt; string(8) "editpost" 
      ["post_author"]=&gt; int(1) 
      ["post_type"]=&gt; string(8) "bookings" 
      ["original_post_status"]=&gt; string(10) "auto-draft" 
      ["referredby"]=&gt; string(79) "http://www.skizzar.com/distractions-login/wp-admin/post.php?post=80&amp;action=edit" 
      ["_wp_original_http_referer"]=&gt; string(79) "http://www.skizzar.com/distractions-login/wp-admin/post.php?post=80&amp;action=edit" 
      ["auto_draft"]=&gt; string(1) "1" 
      ["post_ID"]=&gt; string(2) "81" 
      ["meta-box-order-nonce"]=&gt; string(10) "14fd434c70" 
      ["closedpostboxesnonce"]=&gt; string(10) "2b04c74cd9" 
      ["post_title"]=&gt; string(15) "Test Booking 13" 
      ["samplepermalinknonce"]=&gt; string(10) "4589b08166" 
      ["wp-preview"]=&gt; string(0) "" 
      ["hidden_post_status"]=&gt; string(5) "draft" 
      ["post_status"]=&gt; string(7) "publish" 
      ["hidden_post_password"]=&gt; string(0) "" 
      ["hidden_post_visibility"]=&gt; string(6) "public" 
      ["visibility"]=&gt; string(6) "public" 
      ["post_password"]=&gt; string(0) "" 
      ["mm"]=&gt; string(2) "12" 
      ["jj"]=&gt; string(2) "03" 
      ["aa"]=&gt; string(4) "2015" 
      ["hh"]=&gt; string(2) "11" 
      ["mn"]=&gt; string(2) "05" 
      ["ss"]=&gt; string(2) "06" 
      ["hidden_mm"]=&gt; string(2) "12" 
      ["cur_mm"]=&gt; string(2) "12" 
      ["hidden_jj"]=&gt; string(2) "03" 
      ["cur_jj"]=&gt; string(2) "03" 
      ["hidden_aa"]=&gt; string(4) "2015" 
      ["cur_aa"]=&gt; string(4) "2015" 
      ["hidden_hh"]=&gt; string(2) "11" 
      ["cur_hh"]=&gt; string(2) "11" 
      ["hidden_mn"]=&gt; string(2) "05" 
      ["cur_mn"]=&gt; string(2) "05" 
      ["original_publish"]=&gt; string(7) "Publish" 
      ["publish"]=&gt; string(7) "Publish" 
      ["booking_details"]=&gt; string(10) "54291c7a09" 
      ["wedding_name"]=&gt; string(15) "Test Booking 13" 
      ["wedding_user"]=&gt; string(9) "Test User" 
      ["wedding_date"]=&gt; string(10) "30/12/2015" 
      ["wedding_package"]=&gt; string(9) "Package 1" 
      ["wedding_price"]=&gt; string(5) "45768" 
      ["wedding_payment_due_date"]=&gt; string(10) "28/01/2016" 
      ["wedding_hidden_todays_date"]=&gt; string(10) "03/12/2015" 
      ["wedding_hidden_is_viewed"]=&gt; string(0) "" 
      ["wedding_hidden_is_completed"]=&gt; string(0) "" 
      ["wedding_hidden_date_completed"]=&gt; string(0) "" 
      ["deposit_paid"]=&gt; string(0) "" 
      ["full_paid"]=&gt; string(0) "" 
      ["post_name"]=&gt; string(0) "" 
      ["post_mime_type"]=&gt; string(0) "" 
      ["ID"]=&gt; int(81) 
      ["comment_status"]=&gt; string(6) "closed" 
      ["ping_status"]=&gt; string(6) "closed" 
  } 
  </code></pre>
  
  <p>Below is the code used to render the meta boxes in the custom post type:</p>
  
  <pre><code>public function bookings_meta_boxes() {
      add_meta_box(
          'booking_details',
          'Booking Details',
          array( $this, 'render_meta_boxes' ),
          'bookings',
          'normal',
          'high'
      );
  }
  
  function render_meta_boxes( $post ) {
      wp_nonce_field( basename( __FILE__ ), 'booking_details' ); 
      $prfx_stored_meta = get_post_meta( $post-&gt;ID );
  
  &lt;p&gt;
  &lt;label for="wedding_user" class="prfx-row-title"&gt;&lt;?php _e( 'Wedding Guest', 'prfx-textdomain' )?&gt;&lt;/label&gt;
  &lt;select name="wedding_user" id="wedding_user"&gt;
      &lt;?php
      $users = get_users();
      $i = 0;
      // Array of WP_User objects.
      foreach ( $users as $user ) {
          echo "&lt;option value='".esc_html( $user-&gt;display_name )."' ";
          if (isset($prfx_stored_meta['wedding_user']))
          echo selected($prfx_stored_meta['wedding_user'][0], esc_html( $user-&gt;display_name ));
          echo "&gt;" . esc_html( $user-&gt;display_name ) . "&lt;/option&gt;";
      }
      ?&gt;
  
  &lt;/select&gt;
  &lt;/p&gt;
  
  ...
  
  }
  </code></pre>