dimanche 19 décembre 2021

Choose three emails at random from an indefinite group of emails

it's the first time I'm writing here, sorry for any mistakes. I'm not a programmer, I usually try to study the advice you give on this site and find a way to apply it to my needs but this time I just can't find a solution. I use an email plugin for wordpress that allows me to put a tag like this [emails-group] in the "Bcc" field, this tag can contain one or more email addresses separated by commas. with the code I wrote below (I don't know if it's written in the best way) I can send an email to two random email addresses when the email addresses are three. (this code works in my tests)

add_filter( 'wpcf7_mail_tag_replaced',
  function( $replaced, $submitted, $html, $mail_tag ) {
    if ( 'emails-group' == $mail_tag->field_name() ) {
    foreach($submitted as $value){
        $expl = explode(",", $value);
        $list = array("$expl[0]","$expl[1]","$expl[2]");
        $rand_keys = array_rand($list, 2);
        $first = $list[$rand_keys[0]];
        $second = $list[$rand_keys[1]];
         $result = array("$first","$second");
    $replaced = implode(",",$result);
    }
    }
    return $replaced;
  },
   10, 4
);

what I would need is a new code for:

if the email addresses are one or two or three then send everyone the email, but if the email addresses are more than three choose only three email addresses at random and send them the email.

I thank anyone who can put me on the right way to find the solution. Greetings, Raffaele.




Aucun commentaire:

Enregistrer un commentaire