3

I have a node with a user reference field, in which a user can select unlimited users who they have a relationship with. I'd like for the saving of this node to trigger a separate email to be sent (via Rules) to each user chosen in that reference field, or a single email with all of those users BCC'ed. Currently, the "send mail" action with "node:field-friends-you-wish-to-invite:0:mail" selected in the "To" field of the Rule only sends an email to the first user selected in the list, no matter how many other users have been selected.

How can this be accomplished?

1 Answer 1

1

You should create a rule that looks like so (in Rules export format):

{ "rules_send_email_to_selected_email_ids" : {
    "LABEL" : "Send eMail to selected eMail IDs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : {
      "node_insert--article" : { "bundle" : "article" }
    },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-email-addresses" ] },
          "ITEM" : { "current_email_id" : "Current eMail ID" },
          "DO" : [
            { "drupal_message" : { "message" : "An eMail about this new content with title \u0022[node:title]\u0022 and URL [node:url] will be delivered to [current-email-id:value]." } },
            { "mail" : {
                "to" : "[current-email-id:value]",
                "subject" : "You have new mail ...",
                "message" : "New content was created with title [node:title] ... You can read about it at [node:url].",
                "language" : [ "" ]
              }
            }
          ]
        }
      }
    ]
  }
}

Some more details about the above rule:

  • Rules Event: After saving new content of type "Article" (adapt the type to fit your needs, ie to XYZ).
  • Rules Action: a "loop" for all selected values of a multi value select list (which contains the eMail IDs), with machine name field_email_addresses (adapt the machine name to fit your needs). For each item (= current_email_id), an eMail is created with some subject and message that also contains the title and url of the node being created. Note: this will send 1 separated eMail to each of the selected eMail IDs (there is no way to send a single eMail to all selected eMail IDs).

If you have the Rules UI enabled, you can import the above rule in your own site, after you adapt the machine names of the content type and the field name containing the list of eMail Ids.

After you're convinced the rule works as expected, you may want to remove the Rules Action that displays a message after the node is saved (with drupal_message).

Video tutorial: have a look at the List and loops video which show a similar scenario.

Update

If you don't have such field with eMail IDs, but a field which is an entity reference field to user ids, then you can use a minor variation of the above rule. Instead of field_email_addresses, you'd want to loop over the list of users (indicated via the user reference field, let's assume it's named field_user_ids). And for each "item" (which I'd name "current_userid"), in the "to" field you'd use the current-userid:mail token for the user being processed. Here is how such rule could look like (in Rules export format):

{ "rules_send_email_to_selected_user_ids" : {
    "LABEL" : "Send eMail to selected user IDs",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : {
      "node_insert--article" : { "bundle" : "article" }
    },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-user-ids" ] },
          "ITEM" : { "current_userid" : "Current user ID" },
          "DO" : [
            { "drupal_message" : { "message" : "An eMail about this new content with title \u0022[node:title]\u0022 and URL [node:url] will be delivered to eMail ID [current-userid:mail]." } },
            { "mail" : {
                "to" : "[current-userid:mail]",
                "subject" : "You have new mail ...",
                "message" : "New content was created with title [node:title] ... You can read about it at [node:url].",
                "language" : [ "" ]
              }
            }
          ]
        }
      }
    ]
  }
}
2
  • Thanks for this, but it's not exactly what I described. My users do not know the email addresses of their "friends". That's why I need the rule to interpret the users referenced in the Entity Reference field and then send the emails to those users' account email addresses automatically. In other words, the person creating the content would select "User A" and "User B" from the reference field, and the rule would then send an email to "U[email protected]" and "[email protected]".
    – hockey2112
    Dec 23, 2016 at 16:53
  • 1
    @hockey2112 : OK, I see your point ... Check my expanded answer (the update part of it, near the end). Dec 23, 2016 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.