How to send an email to select set of users for moderation states? We are using the following modules below for content moderation. Currently have we have 5 people with the Content Approver role and all of them are sent an email when a state transition occurs. There is a need to send email alerts of moderation transitions for certain content types that need approval to just 2 out of the 5 Approvers.

How to send an email to select set of users when changing moderation state?

Expected Results:

When a content editor marks a page ready for Review, they are allowed to manually select one or more users to send an email alert to regardless of Role. 

Enabled Modules:


Workbench v8.x-1.0
Workbench Email v8.x-1.0-alpha7
you can maybe use rules module

alternatively create a custom module and you can inspire from this, that is used with core content moderation module: 



use  \Drupal\user\Entity\User;

/**
 *
 *
 * Envoie de mail en fonction des transitions le modération de contenu.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 */
function my_module_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity-&gt;getEntityTypeId() !== 'content_moderation_state') {
    return;
  }

  $article_id = $entity-&gt;get('content_entity_id')-&gt;getString();
  $article = \Drupal\node\Entity\Node::load($article_id);
  if ($article-&gt;bundle() !== 'article') {
    return;
  }

  global $base_url;

  $article_title = $article-&gt;get('title')-&gt;getString();
  $article_url = $base_url . "/node/" . $article-&gt;get('nid')-&gt;getString();

  $author_name = $article-&gt;getOwner()-&gt;get('field_cn_ldap')-&gt;getString();
  $author_email = $article-&gt;getOwner()-&gt;getEmail();
  $current_user_uid = \Drupal::currentUser()-&gt;id();
  $current_user_name = \Drupal\user\Entity\User::load($current_user_uid)
    -&gt;get('field_cn_ldap')
    -&gt;getString();
  $is_author = ($current_user_name == $author_name);    

  $uids = \Drupal::entityQuery('user')
    -&gt;condition('status', 1)
    -&gt;condition('roles', 'redacteur_en_chef')
    -&gt;execute();
  ;
  $users = User::loadMultiple($uids);

  $emails_redacteur_en_chef = [];
  foreach ($users as $user) {
    $emails_redacteur_en_chef[] = $user-&gt;getEmail();
  }
  $emails_redacteur_en_chef = implode(',', $emails_redacteur_en_chef);

  $moderation_state = $entity-&gt;get('moderation_state')-&gt;getString();
  $moderation_state_original = '';
  if ($entity-&gt;original) {
    $moderation_state_original = $entity-&gt;original-&gt;get('moderation_state')
      -&gt;getString();
  }
  $is_transition_change = ($moderation_state !== $moderation_state_original);

  include 'includes/notification/messages.php';
  include 'includes/notification/titles.php';
  $send_to = $author_email;
  $params['node_title'] = $title_mod;
  $params['message'] = $message_same_state;
  $send = FALSE;

  switch ($moderation_state) {
    case 'draft':
      if (!$is_transition_change &amp;&amp; !$is_author) {
        $send = TRUE;
      }
      break;

    case 'relecture':
      if (!$is_transition_change &amp;&amp; !$is_author) {
        $send = TRUE;
      }
      elseif ($is_transition_change) {        
        $send_to = "joe@google.fr";
        $params['node_title'] = $title_prop;
        $params['message'] = $message_prop;
        $send = TRUE;
      }
      break;

    case 'relecture1':
      if (!$is_transition_change &amp;&amp; !$is_author) {
        $send = TRUE;
      }
      elseif ($is_transition_change) {     
        $send_to = "whatever@yahoo.com";
        $params['node_title'] = $title_relec1;
        $params['message'] = $message_relec1;
        $send = TRUE;
      }
      break;

    case 'relecture2':
      $send_to = $emails_redacteur_en_chef;
      $params['node_title'] = $title_relec2;
      $params['message'] = $message_relec2;
      $send = TRUE;
      break;

    case 'published':
      if (!$is_transition_change) {
        $send_to .= ',';
        $send_to .= $emails_redacteur_en_chef;
        $send = TRUE;
      }
      else {

        $send_to = "all@hotmail.com";
        $params['node_title'] = $title_pub;
        $params['message'] = $message_pub;
        $send = TRUE;
      }
      break;

    case 'maj':     
      $send_to = "whatever@wahtever.com";
      $params['node_title'] = $title_maj;
      $params['message'] = $message_maj;
      $send = TRUE;
      break;

    default:
      break;
  }

  if ($send) {
    $mailManager = \Drupal::service('plugin.manager.mail');
    $module = "amu_dud_rules";
    $key = 'state_changed_action';
    $to = $send_to;

    $langcode = \Drupal::currentUser()-&gt;getPreferredLangcode();

    $result = $mailManager-&gt;mail($module, $key, $to, $langcode, $params, NULL, $send);

    if ($result['result'] !== TRUE) {
      drupal_set_message(t('There was a problem sending notifications.'), 'error');
    }
    else {
      drupal_set_message(t("A notification has been sent to $to"));
    }
  }
}

/**
 * Implements hook_mail().
 */
function my_module_mail($key, &amp;$message, $params) {
  $options = [
    'langcode' =&gt; $message['langcode'],
  ];
  switch ($key) {
    case 'state_changed_action':
      //$message['from'] = \Drupal::config('system.site')-&gt;get('mail');
      $from = 'from@from.com';

      $message['from'] = $from;
      $message['headers']['From'] = $from;
      $message['headers']['Sender'] = $from;
      $message['headers']['Return-Path'] = $from;
      $message['subject'] = t('@title', ['@title' =&gt; $params['node_title']], $options);
      $message['body'][] = $params['message'];
      break;
  }
}