Writing custom command plugins (2.x)

Last updated on
30 April 2025

Here is a simple example of a custom Mailhandler command processor. It searches the Subject field of an email for an identifier, and provides it as a mapping source called Case ID for node mapping. Mailhandler command processors are written as Ctools plugins. At the time of writing, Mailhandler is at 2.0-rc1.

The default command plugin is destructive to the $message object. If your plugin doesn't seem to be getting the data you want from $message, lower your plugin's weight in order to run at the desired time. Plugins are ordered by weight in the UI, so you can visually see at what point your plugin will run.

mycustomcommand.info

name = "Sample Command Plugin"
description = "Parses ticket ID from subject line for Mailhandler."
version = "7.x-1.0"
core = "7.x"
dependencies[] = "ctools"
dependencies[] = "feeds"
dependencies[] = "mailhandler"
package = "sample"

files[] = mycustomcommand.module

mycustomcommand.module

/**
 * Implements hook_ctools_plugin_directory().
 */
function mycustomcommand_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'ctools' && $plugin_type == 'commands_plugin') {
    return 'plugins/' . $plugin_type;
  }
  if ($owner == 'mailhandler') {
    return 'plugins/' . $plugin_type;
  }
}

plugins/commands/mycustomcommand.inc

/**
 * @file
 * MailhandlerCommandsMyCustomCommand class.
 */

$plugin = array(
  'name' => 'Sample processor',
  'description' => 'Extracts Case ID from subject line.',
  'handler' => array(
    'class' => 'MailhandlerCommandsMyCustomCommand',
    'parent' => 'MailhandlerCommands',
  ),
  'file' => 'MailhandlerCommandsMyCustomCommand.class.php',
  'weight' => 30,
);

plugins/commands/MailhandlerCommandsMyCustomCommand.class.php file

/**
 * @file
 * MailhandlerCommandsMyCustomCommand class.
 */

class MailhandlerCommandsMyCustomCommand extends MailhandlerCommands {

  /**
   * Parse the subject for a Case ID.
   */
  function process(&$message, $source) {

    $splitmail = array();
    $splitmail = explode("-----(Please reply above this line)-----",$message['origbody']);

    //strip everything after this text
    $message['origbody'] = $splitmail[0];

    //grab case_id from subject line
    $message['case_id'] = substr($message['subject'], strpos($message['subject'], 'case #') + 6);
  }

  function getMappingSources() {
    $sources = array();

      $sources['case_id'] = array(
        'title' => t('Case ID'),
        'description' => t('The original Case ID'),
      );
    return $sources;
  }
}

Help improve this page

Page status: Not set

You can: