2

I use Drupal 7 and I'm testing a function to automatically send email from a custom module.

I have 3 fields to check, and I have to send mail to a specific address.

I have write simple test code in a custom module:

function test_mail_node_presave($node) {
 if($node->type == 'rso') {
  if ($node->field_a['und'][0]['tid'] == '10'){
   mail('example1@mail.com', 'example2@mail.com', 'example text');
  }
 }
}

It works, but in any verification "field_a" Drupal sends 2 equals email.

Note: I'd also be interested to know about any other technique that can achieve the same result.

6
  • edit: with hook_node_submit($node, $form, &$form_state) it is sent 1 mail. What's the reason?
    – Mirkozzo
    Jul 2 '16 at 10:42
  • Why are you using a custom module for this? Why not use an existing (contributed) module that can do this also? Jul 2 '16 at 10:54
  • I have 3 fields to check, and I have to send mail to a specific address. what form does this?
    – Mirkozzo
    Jul 2 '16 at 12:42
  • Are you interested in an answer about a contributed module that can perfectly do what you are asking for? Jul 2 '16 at 12:49
  • I do not know a module that does this
    – Mirkozzo
    Jul 2 '16 at 13:00
0

You could get pretty close to what you're trying to do by simply (and only) using the Rules module. Some more details about this approach:

  • Use Rules Event "Before saving content".
  • Use as many Rules Conditions as needed, for any of your fields you want to validate.
  • Use Rules Action "**Send Mail".

For more details, Have a look at this rule (in Rules export format):

{ "rules_field_validations_before_saving_nodes" : {
    "LABEL" : "Field validations before saving nodes",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_presave--article" : { "bundle" : "article" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_optionlist" } },
      { "data_is" : { "data" : [ "node:field-optionlist" ], "value" : "C" } }
    ],
    "DO" : [
      { "mail" : {
          "to" : "example1@mail.com, example2@mail.com, example text",
          "subject" : "Subject of eMail triggered via Rules",
          "message" : "Message content of eMail triggered via Rules",
          "language" : [ "" ]
        }
      }
    ]
  }
}

Some more details to further explain the above above sample:

  • The Rules event is "After saving new content", but as a sample I've limited it to content type "article" only (omit or adapt that extra verification as needed).
  • In my case I have a selection list field with machine name field_optionlist.
  • This field actually has 3 possible values, like so:
    • A (=Advanced).
    • B (=Basic).
    • C (=Custom).
  • As a sample, I decided to add these Rules Conditions (validations), in the order specified:
    • a validation to ensure that this rule only applies for nodes that have a field with machine name field_optionlist.
    • a validation about attempts to save a node where "The selected value" of field_optionlist equals Custom.
  • If all these Rules Conditions are satisfied, then the Rules Action will send an eMail (feel free to change the eMail subject/body to whatever you want).

If you want, you can use the Rules UI to import the above rule in your own site. But before doing so, make sure you have defined 2 similar fields (with machine names field_optionlist and field_free_format_text). As a variation, adapt the machine names to similar fields you already have (attention about field-optionlist versus field_optionlist though, same for the 2nd field, important!).

If you're not familiar (enough/yet) with Rules, checkout the 32 (!!!) great, and free, video tutorials Learn the Rules framework.

Voilà, the above solution depends on Rules, nothing but Rules ...

2
  • I had already thought of rules, but Rules send 2 equel email at a time and do not allow me an advanced control. I need to send an email to a specific address based on the value of a field, the field has 10 value and for each I associate an email address
    – Mirkozzo
    Jul 4 '16 at 6:55
  • @Mirkozzo Thanks for the feedback, but that slightly changes your question. How about I slightly enhance my answer accordingly? Ie would you be interested in such answer? Jul 4 '16 at 6:58

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.