Send email to users after node update I have a content type "Room" with this config:


Title
Body
field_comments (Comments)
field_members (Users Entity reference)


When I create a room I send with hook_entity_insert() an email to users add to the room

But my problem is when I edit this room and I add New members, I want to send an email only for new.

I try it with this code in custom module with hook_entity_update:

notify.module
/**
* Implements hook_entity_update().
*/
function notify_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
 if ($entity-&gt;getEntityTypeId() !== 'node' || ($entity-&gt;getEntityTypeId() === 'node' &amp;&amp; $entity-&gt;bundle() !== 'room')) {
   return;
 }
 $mailManager = \Drupal::service('plugin.manager.mail');
 $module = ‘notify’;
 $key = 'new_notif';


 //send to all members in room 
 $members = $entity-&gt;get('field_members')-&gt;getValue();
    $tos = [];
    foreach ( $members as $member ) {
        if ($member['target_id'] !== '1') {
            $tid = intval($member['target_id']);
            $user = \Drupal\user\Entity\User::load( $tid );
            $tos[] = $user-&gt;getEmail();
        }
    }

 $membersTo = implode (', ', $tos);

 $to = $membersTo;

 $params['message'] = 'You access to the room';

 $params['node_title'] = $entity-&gt;label();
 $langcode = \Drupal::currentUser()-&gt;getPreferredLangcode();
 $send = true;
 $result = $mailManager-&gt;mail($module, $key, $to, $langcode, $params, NULL, $send);
 if ($result['result'] !== true) {
   drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
 }
 else {
   drupal_set_message(t('Your message has been sent.'));
 }
}


I Don't know how to exclude old members after edit, add new members and save a room.
But my problem is when I edit this room and I add New members, I want
  to send an email only for new.


As hook_entity_update mentions


  Get the original entity object from $entity->original.


$entity-&gt;original gives you access to the values before the update.

So all you gotta do is compare the original members to the updated members to find out who are the new members. 

$og_members = array();
$og_members = $entity-&gt;original-&gt;get('field_members')-&gt;getValue();

$up_members = array();
$up_members = $entity-&gt;get('field_members')-&gt;getValue();

$new_members = array();
// use array_diff() to find the difference.
$new_members = array_diff($up_members, $og_members);