Send message notification when license expired I have a commerce site where I sell subscription using the Commerce License module. I want to send email notification to users whose licenses will expire soon.
I'm trying to do that by creating a custom module. I have 2 code snippets, one to notify users when the license expires, the second to send email using the Message Notify module.
I don't know what variables I should use to have the licence owner's.
This is my custom module and the code works well for me.
    /**
     * Implements hook_cron().
     */
    function license_notif_cron() {
      $expires = strtotime('-1 day');
      $query = new EntityFieldQuery();
      $query
        -&gt;entityCondition('entity_type', 'commerce_license')
        -&gt;propertyCondition('status', COMMERCE_LICENSE_ACTIVE)
        -&gt;propertyCondition('expires', 0, '&lt;&gt;')
        -&gt;propertyCondition('expires', $expires, '&gt;');
      $results = $query-&gt;execute();
      if (!empty($results['commerce_license'])) {
        $license_ids = array_keys($results['commerce_license']);
        drupal_set_message('licensce expires soon');
        //send message
      }
    }

This is an example of sending a message. I am not interested in hook_node_insert(); I already created a new message type (licence_expire_soon).
/*
 * Implements hook_node_insert().
 */
function foo_node_insert($node) {
  $message = message_create('foo_message_type', array('uid' =&gt; $node-&gt;uid));
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper-&gt;field_node_ref-&gt;set($node);
    
  $options = array(
    'rendered fields' =&gt; array(
      'message_notify_email_subject' =&gt; 'field_rendered_subject',
      'message_notify_email_body' =&gt; 'field_rendered_body',
    ),
  );
    
  message_notify_send_message($message, $options);
}
To glue the bits you have together you need to relate your to-be-expiring-soon license with a uid which can feed into message_create. So something like this in your hook_cron call...

//...after assigning $license_ids

foreach($license_ids as $license_id) {

   //...@TODO: checks on previous alert messages

   $uid = db_select('commerce_license','cl')
     -&gt;fields('cl', array('uid'))
     -&gt;condition('license_id', $license_id)
     -&gt;execute()-&gt;fetchField();

   //...@TODO: code for executing message_notify_send_message and sending message to $uid
}