This is typically something you can get to work using the Rules module, together with Rules Scheduler
(a sub-module of it), as explained in the steps below.
Step 1: Enable the Rules Scheduler
Enable the Rules Scheduler
(a sub-module of Rules). And make sure your cron jobs are running (to ensure that scheduled rules actually start executing.
Step 2: Create a Rules "Component"
Create a Rules Component labeled something like Notify all members of a Group, which looks like so (in Rules export format):
{ "rules_notify_all_members_of_a_group" : {
"LABEL" : "Notify all members of a Group",
"PLUGIN" : "action set",
"OWNER" : "rules",
"TAGS" : [ "group" ],
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : {
"group" : { "label" : "Group", "type" : "group" },
"event_node" : { "label" : "Event node", "type" : "node" }
},
"ACTION SET" : [
{ "entity_query" : {
"USING" : {
"type" : "group_membership",
"property" : "group",
"value" : [ "group" ]
},
"PROVIDE" : { "entity_fetched" : { "group_members" : "Group members" } }
}
},
{ "LOOP" : {
"USING" : { "list" : [ "group-members" ] },
"ITEM" : { "member_of_group" : "Member of group" },
"DO" : [
{ "drupal_message" : { "message" : "Group member named \u0022[member-of-group:user:name]\u0022 and eMail Id \u0022[member-of-group:user:mail]\u0022 is a member of the \u0022[group:group-type]\u0022 group titled \u0022[group:title]\u0022 and group id \u0022[group:gid]\u0022, related to the node with id [event-node:nid] and title [event-node:title] (and date [event-node:field-expiration-date])." } },
{ "mail" : {
"to" : "[member-of-group:user:mail]",
"subject" : "eMail notification to group [group:title]",
"message" : "Hello [member-of-group:user:name],\r\n\r\nyou are a member of the group [group:title] (which is of type [group:group-type]).\r\n\r\nYou are receiving this eMail as a friendly reminder related to the node with id [event-node:nid] and title [event-node:title] (and date [event-node:field-expiration-date]).\r\n\r\nGreetings,\r\n\r\nPS: contact the site admin by eMail (via [site:mail]) for more details if needed",
"language" : [ "" ]
}
}
]
}
}
]
}
}
Some more details about this Rules Component:
- The type of this Rules Component is an "Action Set" (no need for any conditions).
- Action 1 is to build a list of all the members of the group. It uses "Fetch entity by property", with these additional specs:
- Entity type value = Group Membership.
- Property value = Group.
- Value of the data selector = Group.
- Limit result count = blank (we do not want to limit the nr of results).
- Provided variables = "Group members" (variable label) and "
group_members
" (variable name).
- Action 2 is to iterate (= loop) over each of the members of the group. It uses "Add a (Rules) loop", with these additional specs:
- The list to loop over =
group-members
- Current list item = "Member of group" (variable label) and "
member_of_group
" (variable name).
- Sub-action (i) of this Action 2 is to just "Show a message on the site" (while QA-testing this Rules Component), with message type = Status and with an appropriate message using various tokens which might be interesting.
- Sub-action (ii) of this Action 2 is to actually send an eMail to the "
member_of_group
" being processed in the loop, using various tokens similar to those in Sub-action (i).
If you have the Rules UI enabled , as well as the Group module, you can simply import the above rule in your own site, no need to tweak anything in it. Though you may want to tune the Subject and Message content of the actual eMail (just remember the various tokens you may want to use), while you might also want to use some other From eMail ID. And after QA-testing it, you probably want to remove the Rules Action "Show a message on the site".
Step 3: Create a "Rule" using the Rules Component
Here is the Rule to be created, in Rules export format (just import it in your own environment via copy-paste):
{ "rules_schedule_group_notification" : {
"LABEL" : "Schedule Group notification",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "rules_scheduler" ],
"ON" : {
"node_insert--article" : { "bundle" : "article" },
"node_update--article" : { "bundle" : "article" }
},
"IF" : [
{ "entity_has_field" : { "entity" : [ "node" ], "field" : "field_event_date" } }
],
"DO" : [
{ "schedule_delete" : {
"component" : "rules_notify_all_members_of_a_group",
"task" : "[node:nid]"
}
},
{ "schedule" : {
"component" : "rules_notify_all_members_of_a_group",
"date" : [ "node:field-event-date" ],
"identifier" : "[node:nid]",
"param_group" : [ "node:group" ],
"param_event_node" : [ "node" ]
}
}
]
}
}
The above rule refers to the Rules Component from the previous step. And is based on these assumptions:
- The content type of the event node is has machine name
article
(adapt prior to import to fit your case).
- The field "Event date" has machine name
field_event_date
(adapt prior to import to fit your case).
The schedule_delete
that is part of this rule is to ensure that previously "scheduled" notifications will get removed when updating the data field (so that it only triggers 1 reminder, and with the most recently entered date).
Attention: whatever gets updated in such node, will each time trigger this rule. Even if the actual date itself is not updated. If that's not what you want, just add additional Rules Conditions so that the rules Actions are only performed if the date is updated.
PS: I'm assuming this question is about D7.