5

I want to send emails using email stored on node fields that are referenced twice by other two contents types. I have this content types relationship:

Content1                 
field1 (unlimited) - reference > Content2    
                                 field2 (only 1) - reference > Content3
                                                               email

I want to send a email message to all itens on field1 using email field on Content3, after the node is created or updated.

I dont have experience with Rules looping. Ill appreciate any help.

Thanks!

  • Rules normally reacts to something happening (i.e. a new node is created) which then causes the email action. What will trigger this rule? – longboardnode Dec 29 '15 at 1:07
  • @longboardnode i know, it can be any...node update, node saved...thats not the problem...i got covered. – Jose San Pedro Dec 30 '15 at 0:39
  • 1
    do you solve the problem? if you are still interested in this, let me know to start a bounty here ;-) – Adrian Cid Almaguer Jan 29 '16 at 21:17
  • 1
    @JoseSanPedro The rule I created uses 3 different content types as well, have you tried using it? Btw, good work on your little scheme, made for a nice visual - so I just went with the most logical setup. – oksana-c Feb 2 '16 at 23:43
  • 1
    @oksana-c i just tested your answer and you nailed it! i reply to you below and marked the answer as accepted, thanks! – Jose San Pedro Feb 3 '16 at 0:06
5
+50

The Rule will react on event "After updating existing content of type Content1".

The Rule will have to utilize a component in order to reach the email field in Content3.

In short: Rule loops through Content1 reference field (may contain multiple references) and then component will process each referenced node of type Content2 and return a variable back to the Rule. Returned variable contains an email that is taken from the field of the node of type Content3.

STEP 1 - Create Rules Component (export below)

{ "rules_process_content2_return_email" : {
    "LABEL" : "Process Referenced Content2 Entity and Return Email",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "content2" : { "label" : "Content2", "type" : "node" },
      "email_address" : { "label" : "Email Address", "type" : "text", "parameter" : false }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "content2" ], "field" : "field_content_reference_2" } },
      { "entity_has_field" : {
          "entity" : [ "content2:field-content-reference-2" ],
          "field" : "field_email"
        }
      }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : {
            "type" : "text",
            "value" : [ "content2:field-content-reference-2:field-email" ]
          },
          "PROVIDE" : { "variable_added" : { "email_address" : "Email Address" } }
        }
      },
      { "data_set" : {
          "data" : [ "email-address" ],
          "value" : [ "content2:field-content-reference-2:field-email" ]
        }
      }
    ],
    "PROVIDES VARIABLES" : [ "email_address" ]
  }
}

STEP 2 - Create the Rule that uses Component above (export below)

{ "rules_send_email" : {
    "LABEL" : "Send Email",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update--content1" : { "bundle" : "content1" } },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-content-reference" ] },
          "ITEM" : { "content2_item" : "Content2 item" },
          "DO" : [
            { "component_rules_process_content2_return_email" : {
                "USING" : { "content2" : [ "content2-item" ] },
                "PROVIDE" : { "email_address" : { "email_address" : "Email Address" } }
              }
            },
            { "mail" : {
                "to" : "[email-address:value]",
                "subject" : "Test Email on Content1 Update",
                "message" : "[node:title] was updated",
                "language" : [ "" ]
              }
            }
          ]
        }
      }
    ]
  }
}

Replace "content1", "content2" and machine names of fields with your own content/fields machine names before importing. Modify to your liking.

  • I'm trying to digest your answer, but I don't see any 3 in your answer, just content 1 and 2. is content2:field-content-reference-2:field-email the line that fetches content 3's e-mail? in your component. – No Sssweat Feb 2 '16 at 2:59
  • @NoSssweat that's right. Thanks for catching that, I just removed "content3" from the list of items to be replaced. – oksana-c Feb 2 '16 at 3:03
  • If they are different content types will this answer work? – No Sssweat Feb 2 '16 at 3:06
  • 1
    Yes. The rule is specifically for 3 different content types as, if I understood correctly, that's what question called for. – oksana-c Feb 2 '16 at 3:09
  • 1
    PS: when Add a variable, you need to click on the (switch to data selection button). Then and only then content2:field-content-reference-2:field-email will become available. – No Sssweat Feb 2 '16 at 3:35
4

I'm a bit confused by your example -- it might help if you said what your content types represent -- but I can tell you in general how to do this.

Let's assume that you want to send your email whenever a Content Type 3 is created or updated. I'm going to guess that Content Type 3 defines the content of an email somehow (i.e., data like the title from the email will be used in the email).

I'm also going to assume that somehow (again, a clearer example would help) that somehow, Content Type 1 and Content Type 2 define an email list to send to.

For your example to work at all, somehow your Type 3, when it changes, needs to have a field that somehow points back to your email list. Let's say that one of your types is an "email group", and one of your types are the emails that belong to each group (this is why we need you to be more clear). Then:

  • You make a rule that triggers on the update and create events of the node.
  • Using data selectors, you create an action loop under actions. Rules data selectors "drill down" into a data type. Since the update and create events get passed the node object, you'll see a "node" item. If you start selecting that node, the selectors will show you all of the node's fields. You want to select the multivalued field that points to, say, the email groups the message wants to send to. Now you have a loop that will take that field and go over each individual slot in that multi-valued field.
  • Add an action to the loop (it's in the middle of the window in small print), OR add a sub-loop that goes from your Type 2 to your Type 1 (whatever makes sense in your example).
  • The action of the inner loop is sending email. You can fill in the fields of the email using data selectors that pull the email fields from the node or one of the sub-items that you created from your loop(s). Note that you can also add tokens into your emails from the "Replacement Patterns" listed under each parameter of the emails.

This is a bit vague. If you'll make your example more concrete, I can make the answer more specific.

You can also send emails using Views Bulk Operations and Rules. But this does not appear to be what you want.

  • I don't think this is what he is looking for, but +1 for your effort. – No Sssweat Feb 2 '16 at 7:49
  • @NoSssweat -- what do you think he wants? I'm confused :-) – Torenware Networks Feb 2 '16 at 7:55
  • If you look at the graph he drew: He has cType#1 (node 1) which has an entity reference field with unlimited values. In that field he is referencing 2 nodes. ex: hello (2), world (3) <-- both of these are cType#2. In hello (node 2) he has an entity reference of max 1 value. ex: cool (4). Cool (node 4) has an e-mail field. So he wants when he creates a cType#1 (node 1) an e-mail gets sent to the address in e-mail field of cType#3 (cool node 4). oksana-c has the correct answer – No Sssweat Feb 2 '16 at 8:45
  • Basically he is referencing a node, which in turn is referencing another node, which is referencing another node that has an e-mail address. – No Sssweat Feb 2 '16 at 8:51

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.