3

I have a content type that contains information about people, if these people want this information forwarded to them, I'd like to be able to capture an email in an email field and send the published content to them in an email.

I've been hunting around for a way to do this but can't find anything. Is there a way to do this?

4
  • How is that info about people contained in that content type? Commented Jul 13, 2016 at 21:24
  • The content type is "Person" and it has a number of fields with machine names like "first-name", "last-name", "image", "date-of-birth". Basically I want to drop the whole node into an email to them.
    – JamiePatt
    Commented Jul 13, 2016 at 21:57
  • To send an email you need email ids. Where do you have those? Commented Jul 13, 2016 at 22:04
  • We have a field "field-notification-email" that stores an email address for each content type.
    – JamiePatt
    Commented Jul 13, 2016 at 22:15

2 Answers 2

1

If you're looking for existing module to base a solution on. The Print module has a Send by email submodule that supplies a form (/printmail/node/NID) that you can tweak for sending out emails and capturing the supplied emails.

/**
 * Implements hook_form_print_mail_form_alter().
 */
function MYMODULE_form_print_mail_form_alter(&$form, &$form_state, $form_id) {
  // Add a custom submission handler for your functionality.
  $form['#submit'][] = 'MYMODULE_form_print_mail_form_submit';
}

function MYMODULE_form_print_mail_form_submit($form, &$form_state) {
  // @TODO: process $form_state['values']['txt_to']['addrs'] for email addresses to store.
  // Use $form_state['values']['path'] to the load relevant node & save data to its fields.
}
0

Below is a sample of a rule (in Rules export format) that will send an eMail "After updating existing content" , and which is similar to what is asked in the question:

{ "rules_notify_author_of_selected_field_updates" : {
    "LABEL" : "Notify author of selected field updates",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } },
      { "NOT data_is" : {
          "data" : [ "node-unchanged:field-optionlist" ],
          "value" : [ "node:field-optionlist" ]
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : "[site:current-user:mail]",
          "subject" : "Notification about a node with a special field value",
          "message" : "Go checkout the node with nid [node:nid] and title [node:title]: it has a field with a value (which is [node:field-optionlist]) you asked to be informed about ..."
        }
      }
    ]
  }
}

Some more details to further explain the above above sample:

  • In my case I have a selection list field with machine name field_optionlist, and a few possible values.
  • The Rules Condition checks if the "The selected value changed". You may want to adapt and/or extend this condition to something like "some eMail field is not blank".
  • If the Rules Condition is true, then (as Rules Action), an eMail will be triggered to the editing user.

Change the field name of field_optionlist to match the field name of your content type, and adapt the actual eMail details (subject, etc) to fit your needs. Then import it in your own site (using the Rules UI). That's it.

PS: If you want to send such eMails to somebody who is not logged in, just change the "to" value to the eMail ID specified in your eMail field in the node. However (red flag!): I would not recommend you to allow this feature for anonymous users. Because spammers would love it: they would only have to edit such nodes, and then type any eMail Id they want to the content of your node to be spammed about ... not a good idea, right?

4
  • Hey Pierre -thanks for this. It's not really a spam issue as permissions for editing this node type are only given to a select group of users. I don't rea;;y understand how to pull the email field from the node - can you give me a little more help with the code?
    – JamiePatt
    Commented Jul 18, 2016 at 2:58
  • @JamiePatt : merci for the clarification that you only want to allow this for registered users (that wasn't part of your question). And to "pull the eMail field": the rule I included in my answer takes that from site:current-user:mail , which means that "the user who's doing the edit of the node, also receives the eMail". If you want to make this a yes/no options for each user, how much flexibility would they have: for all nodes the same yes/no, or for one node 'yes' and another 'no'? Commented Jul 18, 2016 at 8:11
  • Hey Pierre, there's no need for the author of the node to receive the email, just the notification email that's entered into the field on the node. It's basically - if there's an email entered in that field then that email should receive all information from the node.
    – JamiePatt
    Commented Jul 20, 2016 at 20:01
  • @JamiePatt : in that case, just add an extra Rules Condition "entity has field", and check for that actual eMail ID field. If needed, add any extra Rules Condition that may fit to validate the content of that field. Then in the Rules Action, for the "to" eMail field, you should have that eMail Id field available as one of the replacement patterns. so use that instead of "site:current-user:mail". Let me know if you can get it to work like that, ok? Commented Jul 20, 2016 at 20:19

Your Answer

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

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