Using Rules to send out emails

One of the most powerful contributed modules available in Drupal 7 (and Drupal 6) is Rules (http://drupal.org/project/rules).  The following exports are rules I have created to send out email notifications after saving content to everyone who has indicated they want these notifications in a custom field added to the user account.  If it works, you'll receive an email very shortly!

Component action rule:

{ "rules_send_email_after_saving_new_content" : {
    "LABEL" : "Send email after saving new content",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_insert" ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "list\u003cuser\u003e" },
          "PROVIDE" : { "variable_added" : { "all_users" : "All Users" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "all-users" ] },
          "ITEM" : { "current_user" : "Current user" },
          "DO" : [
            { "component_rules_send_email_to_user" : { "user" : [ "current-user" ], "node" : [ "node" ] } }
          ]
        }
      }
    ]
  }
}

Reaction rule:

{ "rules_send_email_after_saving_new_content" : {
    "LABEL" : "Send email after saving new content",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_update", "node_insert" ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "boolean", "value" : 1 },
          "PROVIDE" : { "variable_added" : { "requested_email" : "Requested email indicator" } }
        }
      },
      { "entity_query" : {
          "USING" : {
            "type" : "user",
            "property" : "field_new_post_notification",
            "value" : [ "requested-email" ],
            "limit" : "100"
          },
          "PROVIDE" : { "entity_fetched" : { "users_to_email" : "Users requesting email updates" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "users-to-email" ] },
          "ITEM" : { "user_to_notify" : "User to notify" },
          "DO" : [
            { "component_rules_send_email_to_user" : { "user" : [ "user-to-notify" ], "node" : [ "node" ] } }
          ]
        }
      }
    ]
  }
}

Tags: 

2 Comments

Jamie's picture

Very good

This is all well and good Jonathan but I've read three posts on here now and there hasn't been a single mention of ducks and/or duckpoints. Frankly I feel you are letting the side down and need to do something about it :)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.