Building an Email Notification System for Drupal (2/2)
28 Oct
Jarkko Oksanen
This blog post is about creating a simple notification system using the most robust of the solutions Message stack. This blog post will include step by step instructions which you can then continue to built the system that you truly need.
I’ve kept it very simple, and if you have any questions, feel free to ask them in the comments below. And as in all things Drupal, there is a hundred other ways to go about it.
What we will accomplish:
- Allow users to opt out/in to the notifications
- Notify user for comments to his/her content
This simple functionality can then be expanded upon with anything you wish.
Example with Message stack for the Article content type
In this example we will have a basic Drupal 7 setup, and the content type we're working with is the basic 'Article' content type.
A message is created when one of the events occurs that we stated before. Then we check the type of the message and compare if the user has allowed notifications for this type of message. And if he has, then we send the message.
I’ve exported the rules and also created a feature (module) that you can use to get the example on your own development. Feel free to use it and contribute more to the GitHub repository.
Step five: Add your own messages to the previous rule
From this point forwards you can start building more and more notifications.
- Step one: Create a message type and add type field
Create a message type. To do this navigate to admin/structure/messages and add a message type. I added a message type called message as for this example you need nothing more specific than that.
In this message type, add a text list field and list all the different notifications you would like. Again for simplicity we’re using a text list field instead of an option that you can modify after. If you use this way, remember to add all your settings before adding any content, as it cannot be changed after.
Also add the following arguments to the message type.
- Step two: Set up fields for the user
Add a field to the user profile that will have all the options available. In my case I will be adding a field called “Notify me when for” that will be of type list and will contain all of the notifications stated above. This example is simplified, and can be used for simple setups. You can use the same field you created before, just change the widget to checkbox.
This will list all your notification opt ins
- Step three: Build the rule that creates the messages
This rule creates a message each time a comment is created.
The logic is in the rule:
1. Create entity, message
2. Set data, message type
3. Set data, message title
4. Set data, message body
Here is the rule exported:- { "rules_message_comment" : {
- "LABEL" : "Message: Comment",
- "PLUGIN" : "reaction rule",
- "OWNER" : "rules",
- "REQUIRES" : [ "rules", "comment" ],
- "ON" : { "comment_insert--comment_node_article" : { "bundle" : "comment_node_article" } },
- "DO" : [
- { "entity_create" : {
- "USING" : {
- "type" : "message",
- "param_type" : "message",
- "param_user" : [ "site:current-user" ]
- },
- "PROVIDE" : { "entity_created" : { "message_created" : "Created message" } }
- }
- },
- { "data_set" : {
- "data" : [ "message-created:field-message-type" ],
- "value" : { "value" : { "comment" : "comment" } }
- }
- },
- { "data_set" : {
- "data" : [ "message-created:arguments:title" ],
- "value" : "New comment"
- }
- },
- { "data_set" : {
- "data" : [ "message-created:arguments:body" ],
- "value" : "Your [comment:node] has a new comment!\r\n\r\nBest regards,\r\nCool notify example"
- }
- }
- ]
- }
- }
After this rule is setup, you can see messages forming after comment submission.
- Step four: Build the notify rule
What you need to do after this is to create a rule that sends notifications on message creation and checks that the user that it’s being sent to has the message type notifications set up.
This rule consists of two parts.
The rule that triggers on creation of a message. It is set to trigger the notify rule set mentioned later.- { "rules_message_notify" : {
- "LABEL" : "Message: Notify",
- "PLUGIN" : "reaction rule",
- "OWNER" : "rules",
- "TAGS" : [ "Message" ],
- "REQUIRES" : [ "rules", "message" ],
- "ON" : { "message_insert" : [] },
- "DO" : [
- { "component_rules_message_notification" : { "message" : [ "message" ] } }
- ]
- }
- }
This is the notify set. The logic is the following:
1. Add variable to not send
2. Check that user has the correct notification setting in his profile
3. If yes, send the notification by email
- { "rules_message_notification" : {
- "LABEL" : "Message: Notification",
- "PLUGIN" : "rule set",
- "OWNER" : "rules",
- "TAGS" : [ "Message" ],
- "REQUIRES" : [ "rules", "message_notify" ],
- "USES VARIABLES" : { "message" : { "label" : "message", "type" : "message" } },
- "RULES" : [
- { "RULE" : {
- "PROVIDE" : { "variable_added" : { "variable_added" : "Added variable" } },
- "DO" : [
- { "variable_add" : {
- "USING" : { "type" : "boolean", "value" : "0" },
- "PROVIDE" : { "variable_added" : { "variable_added" : "Added variable" } }
- }
- }
- ],
- "LABEL" : "Provide variables"
- }
- },
- { "RULE" : {
- "IF" : [
- { "entity_is_of_bundle" : {
- "entity" : [ "message" ],
- "type" : "message",
- "bundle" : { "value" : { "message" : "message" } }
- }
- },
- { "data_is" : {
- "data" : [ "message:field-message-type" ],
- "value" : { "value" : { "comment" : "comment" } }
- }
- },
- { "list_contains" : { "list" : [ "message:user:field-message-type" ], "item" : "comment" } }
- ],
- "DO" : [ { "data_set" : { "data" : [ "variable-added" ], "value" : "1" } } ],
- "LABEL" : "Condition: My article has comments"
- }
- },
- { "RULE" : {
- "IF" : [ { "data_is" : { "data" : [ "variable-added" ], "value" : "1" } } ],
- "DO" : [
- { "message_notify_process" : {
- "message" : [ "message" ],
- "save_on_fail" : "0",
- "save_on_success" : "0",
- "mail" : [ "message:user:mail" ]
- }
- }
- ],
- "LABEL" : "Send notification"
- }
- }
- ]
- }
- }
A few other tips
This is a simple example of how you can start building your own email notification system. A few pointers to make it even better.
- Utilize Message subscribe
The message stack comes with message subscribe to allow you implement subscription type notifications.
- Add HTML to your Drupal mails / https://www.drupal.org/project/htmlmail
By default Drupal’s emails are very plain, and if you want to add some color, this is a good way to do it.
drupal planet