I need to send an email notification to followers of a node, when another node that is linked, has been created.


  • 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.

1 Answer 1


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->type == 'blogpost' && $node->is_new) {
    $linked_node_nid = $node->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->join('users', 'u', 'fc.uid = u.uid');
  $query->join('flagging', 'fc2', 'fc2.entity_id=fc.entity_id AND o.entity_type=\'node\' AND fc2.fid=5');
  $query->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->condition('o.field_opt_out_value', 0);
  $or->condition('o.field_opt_out_value', NULL);
  $result = $query
    ->fields('u', array('mail'))
    ->condition('fc.entity_type', 'node')
    ->condition('fc.entity_id', $nid)
    ->condition('fc.fid', 4)
    ->condition('u.status', 1)
    ->orderBy('fc.timestamp', 'DESC')
  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->addVariable('nid', $nid, array('type' => 'integer'));
  $state->addVariable('to', $to, array('type' => 'text'));
  $task = array(
    'identifier' => 'custom_' . $action . '_' . $nid,
    'date' => time(),
    'config' => 'rules_' . $action,
    'state' => $state,

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.