Send email when a user has finished watching something A user watched a video (added listening event to id of video via jwplayer), and a person is notified of said user finishing the video.

jwplayer('some-id-of-video').onComplete( function() {
  //$.ajax or .post whatever to send email to a person to let them know without refreshing page
});


I want to just add this script to this page (this functionality is only needed for this ONE VIDEO). How do I link the PHP code to send the email? Not a form everything will be a standard response.

This  does not work.

$message = array(
  'to' =&gt; 'email address',
  'subject' =&gt; 'subject text',
  'body' =&gt; 'body text',
  'headers' =&gt; 'header text'
);

drupal_mail_send($message);


Where do I put this? Is this wrong? And if it is wrong, what do I need to fix it? How can I call this code from AJAX?
Create an end point for your ajax request to call using hook_menu(). In this example I'm passing the UID of the user as an argument.

E.g:

/**
* Implementation of hook_menu().
*/
function myModule_menu() {
  $items['mymodule/send-email/%user'] = array(
    'page callback' =&gt; 'myModule_ajax_send_email',
    'page arguments' =&gt; array(2),
    'type' =&gt; MENU_CALLBACK,
    'access arguments' =&gt; array('access content'),
  );
  return $items;
}


The callback for your menu item will be responsible for actually sending the email. See this question: How to send email in drupal

E.g.:

function myModule_ajax_send_email($user) {

  // Here you put the code to actually send the email
  $status = some_logic_to_check_email_sent();

  drupal_json(array('status' =&gt; $status));
}


So then once your video has finished playing you just need to make an ajax request to your new end point, passing any information required as parameters.
E.g:

(function($) {
    Drupal.behaviors.myModule = {
        'attach': function(context) {

            jwplayer('some-id-of-video').onComplete(function(){ 
              //$.ajax or .post whatever to send email to a person to let them know without refreshing page 
              var UID = 1; // actually set UID here
              var url = 'mymodule/send-email/' + UID;
              $.get(url, null, emailCallback);
            });

        }
    }
    var emailCallback = function(response) {
        var result = Drupal.parseJson(response);
        if(result.status !== 1) {
          // Some code to let user know there was an error
        }
    }
})(jQuery);