2

I am using the Rules module to send an email when a content node gets saved/updated.

I have a basic node where users can upload a large number of files and enter an email address to notify someone about the change.

In this example, the Rules Action to Send Email is in a Rules loop, and I get one email per item in list. When I move Send Email to the left, I cannot access the "list_item" variable.

Rules Events:

  • After saving new content
  • After updating existing content

Rules Conditions:

  • Content is of type, Parameter: Content: [node], Content types: BasicPage

  • NOT Data value is empty, Parameter: Data to check: [node:field-email]

Rules Actions:

  • Loop

  • Parameter: List: [node:field-file]

  • List item: Current list item (list_item)

    • Send Email
    • Parameter: To: [node:field-email], Subject: Test [node:title], Message: [list-item:file:url], From: noreply@host.ca
  • Merci for the (late) accept, better late then never, right? – Pierre.Vriens Jan 26 '17 at 15:05
0

Problems with what you tried so far

the Rules Action to Send Email is in a Rules loop, and I get one email per item in list

Your rule works as implemented: for each iteration of your loop, you have a Send an eMail action ... That's not really what you want of course ...

When I move Send Email to the left, I cannot access the "list_item" variable.

With that "move Email to the left", you must mean something like "Using the Rules UI, I change the Rules Action to Send an eMail, so that it is no longer considered as one of the Rules Actions to be performed for each iteration of the loop. And instead that Send an eMail Rules Action should only be performed 1 time after the Rules loop is completed finished".

So yes, that's a way to only execute such Send an eMail Rules Action 1 time (instead of for each iteration of the loop). However, by doing so, your Rules Action to Send an eMail no longer has access to that crucial variable list_item which is like a local variable inside your loop.

Solution to make it work anyway

However, if what you're looking for (= the list of files as stored in your file field) is not available, then just (a) make it available first and (b) wait with your Send an eMail action until it is available. Read on for more details ...

The trick to do so is similar to what is shown in the Rules example included in my answer to "How to concatenate all token values of a list in a single field within a Rules loop?". In your case, you could make it work like so:

  1. Add some new Rules variable that will be used later on as part of the eMail body, before the start of your loop. Say you name the variable file_list_var_for_email_body.
  2. Within your loop, for each iteration, prepend or append the value for each list_item to that variable file_list_var_for_email_body (see where we're going???).
  3. Move that Send an eMail action outside your loop, and after the loop completed. And finetune the details (configuration) of your Rules Action to Send an eMail. When doing so, you'll be able to select the token for file_list_var_for_email_body to include anywhere in your eMail body.

Easy, no?

0

Concatenating text in Rules was difficult when I did it. What I did was to keep two variables: a text array with a starting element value (such as "This is a digest:...") and a text value.

For each iteration of a loop over items to concatenate into a digest, etc. I set the text value to be the first item in the array ([array:0]) and then set the [array:0] value to be a concatenation of the text value and the new value from the loop. When the loop finishes, you know the [array:0] has the digest content.

I do not know why this couldn't work in a simpler way by concatenating the text value each loop directly or using two text values instead of an array. When I tried either of the other ways it only kept the last item or first item of what was being concatenated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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