Sending email notification based on flagged node being linked via entity reference I need to send an email notification to followers of a node, when another node that is linked, has been created.

Example:


Users follow "location" type (e.g London) by flagging that node using
the flag module and 'follow' type.
Author creates a "blog" type (e.g. Things to do 2014!) and links it to their location via field_location [entity reference].
When published, send email to all followers of
the "location" type.


Can anyone recommend the way to do this with rules or point me in the right direction?

Thanks,
Paul.
I found the answer to this, or at least, a way to do this. In a custom.module:

Firstly, this checks if something is required to happen (creating a 'blogpost'), and if so, then gets the NID of the item the user is following (e.g. field_linked_item).

function custom_node_presave($node) {
if ($node-&gt;type == 'blogpost' &amp;&amp; $node-&gt;is_new) {
    $linked_node_nid = $node-&gt;field_linked_item['und'][0]['target_id'];
    if ($linked_node_nid) {
      $emails = custom_get_blog_follower_emails($linked_node_nid);
      if ($emails) {
        custom_schedule_mass_email($linked_node_nid, implode(',', $emails), 'email_blog_post_linked');
      }
    }
  }
}


This function gets the email address for all users who flagged the blog that is linked to this blogpost (via linked_item) and returns the list to the function above:

function custom_get_blog_follower_emails($nid) {
  $query = db_select('flagging', 'fc');
  $query-&gt;join('users', 'u', 'fc.uid = u.uid');
  $query-&gt;join('flagging', 'fc2', 'fc2.entity_id=fc.entity_id AND o.entity_type=\'node\' AND fc2.fid=5');
  $query-&gt;leftJoin('field_data_field_opt_out', 'o', 'o.entity_id=u.uid AND o.entity_type=\'user\' AND o.deleted=0');
  $or = db_or();
  $or-&gt;condition('o.field_opt_out_value', 0);
  $or-&gt;condition('o.field_opt_out_value', NULL);
  $result = $query
    -&gt;fields('u', array('mail'))
    -&gt;condition('fc.entity_type', 'node')
    -&gt;condition('fc.entity_id', $nid)
    -&gt;condition('fc.fid', 4)
    -&gt;condition('u.status', 1)
    -&gt;condition($or)
    -&gt;orderBy('fc.timestamp', 'DESC')
    -&gt;execute()
    -&gt;fetchCol();
  return $result;
}


If email addresses are found by the function above, the original function then uses this to send emails via a pre-created rules message:

function custom_schedule_mass_email($nid, $to, $action) {
  $state = new RulesState();
  $state-&gt;addVariable('nid', $nid, array('type' =&gt; 'integer'));
  $state-&gt;addVariable('to', $to, array('type' =&gt; 'text'));
  $task = array(
    'identifier' =&gt; 'custom_' . $action . '_' . $nid,
    'date' =&gt; time(),
    'config' =&gt; 'rules_' . $action,
    'state' =&gt; $state,
  );
  rules_scheduler_schedule_task($task);
}


You need to create a rule to accept the 'to' and 'nid' (nid in my use is to allow rules to get some information about that post). In my case, the rule is called 'email_blog_post_linked'.

This is pretty specific to my requirements, but hopefully it will point people in the right direction.