How to avoid sending 2 emails at the same time? 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-&gt;type == 'rso') {
  if ($node-&gt;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.
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 ...