title: How to send wp_mail on button click?
body: <p>I'm building an options page in my WordPress plugin, I've got a form with to, from, subject &amp; message with a save and send button.</p>
  
  <p>The idea is you fill in the to, from, subject and message click save then click send to send the message. I'm having trouble trying to run the send function on button click.</p>
  
  <p>I've checked out: <a href="https://stackoverflow.com/questions/8597846/wordpress-plugin-call-function-on-button-click-in-admin-panel">Wordpress Plugin: Call function on button click in admin panel</a></p>
  
  <p>Also checked out <a href="https://wordpress.stackexchange.com/questions/42471/is-this-the-right-way-to-execute-some-php-in-my-plugin-after-a-button-click-on-a">Is this the right way to execute some PHP in my plugin after a button click on an options page?</a></p>
  
  <p>My code is as follows:</p>
  
  <pre><code>add_action('admin_menu', 'custom_mail_menu');
  function custom_mail_menu() {
  
  //create new top-level menu
  add_options_page('Custom Mail Settings', 'Custom Mail Settings', 'administrator', __FILE__, 'custom_mail_settings_page');
  
  //call register settings function
  add_action( 'admin_init', 'custom_mail_settings' );
  }
  
  function custom_mail_settings() {
  //register our settings
  register_setting( 'custom-mail-settings-group-15167', 'custom_mail_to' );
  register_setting( 'custom-mail-settings-group-15167', 'custom_mail_from' );
  register_setting( 'custom-mail-settings-group-15167', 'custom_mail_sub' );
  register_setting( 'custom-mail-settings-group-15167', 'custom_mail_message' );
  }
  
  function sendMail() {
  $sendto = esc_attr( get_option('custom_mail_to') );
  $sendfrom =  esc_attr( get_option('custom_mail_from') );
  $sendsub = esc_attr( get_option('custom_mail_sub') );
  $sendmess = esc_attr( get_option('custom_mail_message') );
  $headers = "From: Wordpress &lt;" . $sendfrom . "&gt;";
  wp_mail($sendto, $sendsub, $sendmess, $headers);
  
  }
  
  function custom_mail_settings_page() {
  
  if (!current_user_can('manage_options'))  {
      wp_die( __('You do not have sufficient pilchards to access this page.')    );
  }
  
  
  ?&gt;
  &lt;div class="wrap"&gt;
  &lt;h2&gt;Custom Mail Settings&lt;/h2&gt;
  
  &lt;form method="post" action="options.php"&gt;
      &lt;?php settings_fields( 'custom-mail-settings-group-15167' ); ?&gt;
      &lt;?php do_settings_sections( 'custom-mail-settings-group-15167' ); ?&gt;
      &lt;table class="form-table" style="width: 50%"&gt;
          &lt;tr valign="top"&gt;
              &lt;th scope="row"&gt;To&lt;/th&gt;
              &lt;td&gt;
                  &lt;input style="width: 100%" type="text" name="custom_mail_to" value="&lt;?php echo esc_attr( get_option('custom_mail_to') ); ?&gt;" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
  
          &lt;tr valign="top"&gt;
              &lt;th scope="row"&gt;From&lt;/th&gt;
              &lt;td&gt;
                  &lt;input style="width: 100%" type="text" name="custom_mail_from" value="&lt;?php echo esc_attr( get_option('custom_mail_from') ); ?&gt;" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
  
          &lt;tr valign="top"&gt;
              &lt;th scope="row"&gt;Subject&lt;/th&gt;
              &lt;td&gt;
                  &lt;input style="width: 100%" type="text" name="custom_mail_sub" value="&lt;?php echo esc_attr( get_option('custom_mail_sub') ); ?&gt;" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr valign="top"&gt;
          &lt;th scope="row"&gt;Message&lt;/th&gt;
              &lt;!-- &lt;td&gt;&lt;input type="text" name="custom_mail_message" value="?php echo esc_attr( get_option('custom_mail_message') ); ?&gt;" /&gt;&lt;/td&gt; /--&gt;
          &lt;td&gt;
              &lt;textarea style="text-align: left;" name="custom_mail_message" rows="10" cols="62"&gt;&lt;?php echo esc_attr( get_option('custom_mail_message') ); ?&gt;&lt;/textarea&gt;
          &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?php submit_button('Save'); ?&gt;&lt;td&gt;&lt;?php submit_button('send'); ?&gt;&lt;/td&gt;&lt;/td&gt;
  
          &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>
  
  <p>So my question is: How would I run the <code>sendMail()</code> function when the send button is clicked in my form with the supplied values from the form ?</p>