After being tasked with developing a simple solution to send messages to any user or group of users on a Drupal site, I looked to views bulk operations, rules, message, and message notify module. The three components that you'll need to create are; a new message type, a new rules component, and a new view. That's it! Below I give a brief step-by-step guide that requires that you're already familiar with these modules to follow. I'm also including exports of each component that you can import right into the respective module for a solution out of the box.
Make sure you have message, message notify, views, views bulk operations, and rules installed on your site.
Create your message type
You can use the message type settings boxes to compose your subject and body directly, or you can use something like an entity reference to a node. In our case, we'll use an entity reference to any node and then use tokens so that the node title becomes the email subject and the node body becomes the email body. This allows us to send any node to any user (or group of users) as an email.
- Visit admin/structure/messages and create a new message type.
- Create two values for message text. The first on will be the message subject and the second the body.
- Save your message type settings with any text in the required message text boxes (we'll replace later).
- Click on manage fields.
- Create an entity reference field called "field_message_node" that references nodes and select any applicable bundles that you want to filter by (or leave unselected).
- Save your field settings and switch back over to the edit tab of the message type.
- Enter the token [message:field-message-node:title] as your subject (first box)
- Enter the token [message:field-message-node:body] as your body (second box)
- Save the message type
Create the rules component
Next you'll want to create a rules component that creates a new message entity, assigns it to any user on the site, and executes a message notification.
- Visit admin/config/workflow/rules/components and create a new "rule" component
- Add two required parameters to the rule component. One for user and one for node/content.
- Add the following actions to your rule component
- Create a new entity (fill in the fields with the message type and user that the message should be assigned to)
- Set a data value (set the field_message_node on the message entity to the node parameter passed to the component)
- Save entity (save the message entity)
- Send Message with Message notify (leave all fields as default)
Create a user view
The final step is to create a page user view that contains a views bulk operations field. Make sure you select the newly created rules component as the bulk operations action. Since you're using views you can add any filters for searching and filtering out the users that you want to message.
See it in action
Once you're done, visit the view page that you created. Select the users you want to send a message to and select the operation for sending it from the bulk operations drop down. The following page will be a text box for entering the node id of the content that will be used for the subject and body of the message. The next page is a confirmation and the final page is the bulk operation batch that will loop through every user you selected and send them the contents of the node as an email.
I've already used this on several sites so that administrators can send messages to users and even so other users can communicate with each other. The node reference can be replaced with a form or any input that allows the user to compose the subject and body of the message. The messages are also saved as entities, so you could even create a view that shows the users all of their messages.
Here are gists of each export so you can get started:
Imports the message type. Make sure you add the field_message_node entity reference after importing
https://gist.github.com/andyg5000/08114e82de90b41ce9c5
{
"name" : "marketing_message",
"description" : "Marketing Message",
"argument_keys" : [],
"argument" : [],
"category" : "message_type",
"data" : {
"token options" : { "clear" : 0 },
"purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
},
"language" : "en",
"arguments" : null,
"message_text" : { "en" : [
{
"value" : "[message:field-message-node:title]",
"format" : "plain_text",
"safe_value" : "\u003Cp\u003E[message:field-message-node:title]\u003C\/p\u003E\n"
},
{
"value" : "[message:field-message-node:body]",
"format" : "plain_text",
"safe_value" : "\u003Cp\u003E[message:field-message-node:body]\u003C\/p\u003E\n"
}
]
},
"rdf_mapping" : []
}
Imports the rules component
https://gist.github.com/andyg5000/c914af0e2fbed37bc005
{ "rules_create_and_send_marketing_message_to_user" : {
"LABEL" : "Create and send marketing message to user",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "message_notify" ],
"USES VARIABLES" : {
"user" : { "label" : "User", "type" : "user" },
"node" : { "label" : "Node", "type" : "node" }
},
"DO" : [
{ "entity_create" : {
"USING" : {
"type" : "message",
"param_type" : "marketing_message",
"param_user" : [ "user" ]
},
"PROVIDE" : { "entity_created" : { "message" : "Message" } }
}
},
{ "data_set" : { "data" : [ "message:field-message-node" ], "value" : [ "node" ] } },
{ "entity_save" : { "data" : [ "message" ] } },
{ "message_notify_process" : { "message" : [ "message" ] } }
]
}
}
Creates a view /admin/people/message which loads all users and exposes the bulk operations
https://gist.github.com/andyg5000/c80447facb3655c1351e
$view = new view();
$view->name = 'people_message';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'People Message';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'People Message';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'administer users';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '50';
$handler->display->display_options['style_plugin'] = 'table';
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
/* Field: Bulk operations: User */
$handler->display->display_options['fields']['views_bulk_operations']['id'] = 'views_bulk_operations';
$handler->display->display_options['fields']['views_bulk_operations']['table'] = 'users';
$handler->display->display_options['fields']['views_bulk_operations']['field'] = 'views_bulk_operations';
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['display_type'] = '0';
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['enable_select_all_pages'] = 1;
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['force_single'] = 0;
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['entity_load_capacity'] = '10';
$handler->display->display_options['fields']['views_bulk_operations']['vbo_operations'] = array(
'action::user_block_user_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::views_bulk_operations_user_cancel_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'rules_component::rules_create_and_send_marketing_message_to_user' => array(
'selected' => 1,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::views_bulk_operations_delete_item' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::views_bulk_operations_delete_revision' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::views_bulk_operations_script_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::flag_user_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::views_bulk_operations_modify_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
'settings' => array(
'show_all_tokens' => 1,
'display_values' => array(
'_all_' => '_all_',
),
),
),
'action::views_bulk_operations_user_roles_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::views_bulk_operations_argument_selector_action' => array(
'selected' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
'settings' => array(
'url' => '',
),
),
'action::system_send_email_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::panelizer_set_status_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::realname_action_realname_update' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::pathauto_user_update_action' => array(
'selected' => 0,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
);
/* Sort criterion: User: Created date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'users';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: User: Active */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'users';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = '1';
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Display: Message */
$handler = $view->new_display('page', 'Message', 'page');
$handler->display->display_options['path'] = 'admin/people/message';
$translatables['people_message'] = array(
t('Master'),
t('People Message'),
t('more'),
t('Apply'),
t('Reset'),
t('Sort by'),
t('Asc'),
t('Desc'),
t('Items per page'),
t('- All -'),
t('Offset'),
t('« first'),
t('‹ previous'),
t('next ›'),
t('last »'),
t('Name'),
t('User'),
t('- Choose an operation -'),
t('Message'),
);