Main navigation

Send emails with VBO

0 coding involved!

Use views bulk operations to send email messages to your users

Posted:Tue, July 1st, 2014

Author:andyg5000

Tags:viewsDrupal Planet

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.

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.

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